Bean validation not firing in my Spring Batch project with Spring version 4.1.7.RELEASE and Spring Batch version 3.0.4.RELEASE - spring

Here is my test case, it is throwing the expected org.springframework.batch.item.validator.ValidationException. The commentor below helped and so did http://springbatchbyexample.blogspot.com/2014/09/validation-input-data.html
#Autowired
MdwValidatingItemProcessor mdwValidatingItemProcessor;
#Test(expected = ValidationException.class)
public void notNullOnProperty(){
MecMdw mecMdw = new MecMdw();
//nothing set, property "lob" is null
mdwValidatingItemProcessor.process(mecMdw);
}
I have the following domain object that is using javax.validation.constraints.NotNull:
import javax.validation.constraints.NotNull;
public class MecMdw {
#NotNull
private String lob;
The versions of Spring and Spring Batch are:
<spring.version>4.1.7.RELEASE</spring.version>
<spring.batch.version>3.0.4.RELEASE</spring.batch.version>
I have the following libraries added to my pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
And in my Spring Batch job XML I have wired up the validator so:
<bean id="mdwValidatingItemProcessor" class="com.my.itemprocessor.MdwValidatingItemProcessor">
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</property>
</bean>
The MdwValidatingItemProcessor looks as follows, again for this test case, I am able to enter into the process method, while debugging:
public class MdwValidatingItemProcessor implements ItemProcessor<MecMdw, MecMdw> {
private Validator validator;
public void setValidator(Validator validator) {
this.validator = validator;
}
#Override
public MecMdw process(MecMdw item) throws Exception {
BindingResult results = BindAndValidate(item);
if (results.hasErrors())
buildValidationException(results);
//TODO write to kickout table
return item;
}
private BindingResult BindAndValidate(MecMdw item) {
DataBinder binder = new DataBinder(item);
binder.setValidator(validator);
binder.validate();
return binder.getBindingResult();
}
private void buildValidationException(BindingResult results) {
StringBuilder msg = new StringBuilder();
for (ObjectError error : results.getAllErrors()) {
msg.append("-*-*-*- \n" + error.toString() + "-*-*-*- \n");
}
throw new ValidationException(msg.toString());
}
This came courtsey of http://springbatchbyexample.blogspot.com/2014/09/validation-input-data.html

Related

Powermock calling actual private method instead of mocking

I want to mock the private method "downloadFromNexus" but instead of mocking, actual method gets called while trying to mock here PowerMockito.doReturn("").when(spy, "downloadFromNexus", "", "");
#Component(value = "DownloadXFile")
#Order(1)
#Slf4j
public class DownloadXFile implements DownloadJarFiles {
#Value("${path}")
private String path;
#Override
public void download() throws IOException {
....
downloadFromNexus(path, outputFilePath);
log.info("jar {} downloaded", jar);
}
private void downloadFromNexus(final String url, final String outputFilePath) throws IOException {
FileUtils.copyURLToFile(
new URL(url),
new File(outputFilePath),
2000,
2000);
}
}
Test
#RunWith(PowerMockRunner.class)
#PrepareForTest(DownloadXFile.class)
#SpringBootTest(classes = DownloadXFile.class)
class DownloadXTest {
#Autowired
DownloadXFile downloadXFile;
#Test
public void test() throws Exception {
final DownloadXFilespy = PowerMockito.spy(downloadXFile);
PowerMockito.doReturn("").when(spy, "downloadFromNexus", "", "");
downloadXFile.download();
PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke("downloadFromNexus");
}
}
pom.xml
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.42-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
You called downloadXFile.download(); on original object instead of the spy.

Request method of type post not recognized by Spring Rest Api

This Code work fine :
#RestController
#RequestMapping("/api/post")
public class PostController {
#Autowired
private PostService postService;
public PostService getPostService() {
return postService;
}
public void setPostService(PostService postService) {
this.postService = postService;
}
#RequestMapping(value = "/userPost", method = RequestMethod.GET)
#ResponseBody
public String userPost(String username) {
try {
List<Validation> validations = new ArrayList<Validation>();
ValidationHandeler.rejectIfEmptyOrWhitespace(validations, username, "username is requierd");
if (validations.isEmpty()) {
List<Post> followers = getPostService().findUserPost(username);
return new Message(followers, MessageSuccesStatusEnum.SUCCESS).toString();
}
return new Message(validations, MessageSuccesStatusEnum.FAILED).toString();
} catch (Exception ex) {
return new Message("Error retriving follower list : " + ex.toString(), MessageSuccesStatusEnum.FAILED)
.toString();
}
}
and this is the output :
{"result":"0","info":["username is requierd"]}
but when ever I change the RequestMethod type to post it give me the following output :
{"timestamp":1492948640973,"status":404,"error":"Not Found","message":"No message available","path":"/api/post/userPost"}
does any body know how to fix this problem?
Looks like you are using GET instead of POST as a request method.
#RequestMapping(value = "/userPost", method = RequestMethod.GET)
<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
TestClass TestNG
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class basics {
#Test
public void getPlaceAPI()
{
RestAssured.baseURI = "https://maps.googleapis.com";
given().param("location", "42.3675294,-71.186966").param("radius",
"10000").param("key", "").when()
.get("/maps/api/place/textsearch/json").then().assertThat().statusCode(200);
}
}

Error while connecting to AWS SQS from spring boot

I am trying to integrate AWS SQS into my springboot app using spring cloud AWS, but keep getting this error(posted below), can someone help?
Here are my files.
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'simpleMessageListenerContainer' defined in
class path resource
[org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]:
Invocation of init method failed; nested exception is
java.lang.NoSuchMethodError:
com.amazonaws.http.ExecutionContext.setCredentials(Lcom/amazonaws/auth/AWSCredentials;)V
#Configuration
public class AWSConfig {
#Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
#Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
#Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
#Value("${amazon.sqs.endpoint}")
private String amazonSqsEndpoint;
#Bean
#Primary
public AmazonSQSAsyncClient amazonSQSAsyncClient() {
AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);
}
return amazonSQSAsyncClient;
}
#Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}
}
I am able to work with dynamodb with this but not able to connect to SQS.
I have given the correct access key, secret access key and end point in application.properties file.
#Component
#EnableSqs
public class SQSDao {
private static final Logger logger = LoggerFactory.getLogger(SQSDao.class);
private QueueMessagingTemplate queueMessagingTemplate;
#Autowired
public SQSDao(AmazonSQSAsync amazonSqs) {
this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
}
public void send(String message) {
System.out.println(queueMessagingTemplate.getDefaultDestination());
queueMessagingTemplate.convertAndSend("test-queue", MessageBuilder.withPayload(message).build());
}
#SqsListener(value = "test-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receive(String message)
{
System.out.println("message: " + message);
}
}
I was facing the same issue as described. My solution requeried implement some extra methods for the Config class:
imports [...]
#Configuration
#RefreshScope
public class SpringCloudSQSConfig {
#Value("${cloud.aws.credentials.accessKeyId:default}")
private String accessKeyId;
#Value("${cloud.aws.credentials.secretKey:default}")
private String secretKey;
#Value("${cloud.aws.region.static:default}")
private String region;
private Logger logger = LoggerFactory.getLogger(this.getClass());
#Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSQSAsync());
}
public AmazonSQSAsync amazonSQSAsync() {
return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.US_EAST_2)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)))
.build();
}
#Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
msgListenerContainerFactory.setAmazonSqs(amazonSQSAsync());
return msgListenerContainerFactory;
}
#Bean
public QueueMessageHandler queueMessageHandler() {
QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
queueMsgHandlerFactory.setAmazonSqs(amazonSQSAsync());
QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
List<HandlerMethodArgumentResolver> list = new ArrayList<>();
HandlerMethodArgumentResolver resolver = new PayloadArgumentResolver(new MappingJackson2MessageConverter());
list.add(resolver);
queueMessageHandler.setArgumentResolvers(list);
return queueMessageHandler;
}
}
And for the dependencies implemented:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
and adding the next property in the properties file:
spring.main.allow-bean-definition-overriding=true
I was able to fix this problem by adding the last line shown to my application.yml during local testing
spring:
autoconfigure:
exclude:
- org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
I resolved it by removing duplicate dependencies :
aws-xray-recorder-sdk-aws-sdk
aws-xray-recorder-sdk-aws-sdk-v2
aws-xray-recorder-sdk-aws-sdk-instrumentor
aws-xray-recorder-sdk-aws-sdk-v2-instrumentor
Before :
<!-- AWS X-Ray -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql-postgres</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-spring</artifactId>
</dependency>
After:
<!-- AWS X-Ray -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql-postgres</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-spring</artifactId>
</dependency>
Fixed it, apparently issue was related to dependency mix up as mentioned here

Spring web app returns HTTP Status 406

I'm creating a basic spring based web app:
pom dependencies:
<properties>
<java-version>1.8</java-version>
<springframework-version>4.3.3.RELEASE</springframework-version>
<jackson-version>2.8.3</jackson-version>
<org.slf4j-version>1.7.6</org.slf4j-version>
<logback.version>1.1.7</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- #Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
In order to skip the usage of web.xml I'm using WebApplicationInitializer:
public class AppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(SpringModule.class.getPackage().getName());
return context;
}
Here is my spring config class:
#Configuration
#ComponentScan(basePackages = "com.company.app")
public class SpringModule extends WebMvcConfigurerAdapter {
public SpringModule() {
super();
}
private MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(customJackson2HttpMessageConverter());
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
super.configureMessageConverters(messageConverters);
}
/*
* Configure ContentNegotiationManager
*/
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.ignoreAcceptHeader(true).defaultContentType(
MediaType.APPLICATION_JSON);
}
Here is my test controller:
#Controller
#RequestMapping(value = "/user")
public class SomeController {
#RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public Person helloElad() {
return new Person("some name");
}
}
When testing the controller (using browser) I'm getting:
If I'm returning a plain String it works fine.
I tries to debug method configureMessageConverters and configureContentNegotiation but for some reason it never gets there (on bootstrapping), I'm not sure it is related to the problem though.
Any ideas?
HTTP 406 is an indication that your request content is not negotiated, it is probably that necessary http message converters are not found in configuration. Simple way to add basic set of message converter would be annotating your controller #EnableMvc
I had the same problem and couldn't find a solution here or elsewhere.
I also tried the advice of applying #EnableMvc on my AppConfig but that caused a different problem in which Tomcat wouldn't even successfully start up.
Eventually, I had to rewrite my AppInit class as follows:
https://github.com/viralpatel/spring4-restful-example/tree/master/src/main/java/net/viralpatel/spring/config
Now, I'm getting JSON back when I return a POJO. I don't like this fix. The code seems incomplete compared to the AppInit shown in the problem here, but I'm unstuck.

Custom exception mapping for JAX-RS Bean validation

I am building a REST web service using Apache CXF, Spring and JAX-RS, where I need to send custom exceptions when input JSON validation fails.
Instead of using business logic, I am trying to use Out-of-the-box bean validation feature of CXF, JAX-RS.
Bean validation works fine, however, it always throws 500 exception, which is less than useful. As per documentation, it is the expected behavior of org.apache.cxf.jaxrs.validation.ValidationExceptionMapper.
I am trying to extend exception mapper so that I can throw different error message, but this custom mapper is not getting called.
Below is my code setup ->
pom.xml
<!-- Spring Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.3</version>
</dependency>
<!-- Hibernate validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<!-- JSR 303 Validation framework -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.19</version>
</dependency>
JAX-RS server
<jaxrs:server address="/" id="iamService">
<jaxrs:serviceBeans>
<bean class="com.XXXXXX.iam.service.core.endpoint.impl.IAMIdentityServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<ref bean="validationInInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="validationOutInterceptor" />
<ref bean="exceptionMapperOutInterceptor" />
</jaxrs:outInterceptors>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
<jaxrs:providers>
<ref bean='jsonProvider' />
<ref bean="ampfExceptionMapper"/>
</jaxrs:providers>
</jaxrs:server>
<bean id="ampfExceptionMapper" class="com.xxxxxxx.iam.service.core.interceptor.AmpfValidationExceptionMapper"/>
<bean id="exceptionMapperOutInterceptor" class="org.apache.cxf.jaxrs.interceptor.JAXRSOutExceptionMapperInterceptor"/>
<bean id="validationProvider" class="org.apache.cxf.validation.BeanValidationProvider" />
<bean id="validationInInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor">
<property name="provider" ref="validationProvider" />
</bean>
<bean id="validationOutInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor">
<property name="provider" ref="validationProvider" />
</bean>
Custom Exception mapper
#Provider
#Produces(MediaType.APPLICATION_JSON)
public class AmpfValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException>{
#Override
public Response toResponse(ConstraintViolationException exception) {
System.out.println("Executing Ameriprise Service exception mapping");
UniqueIDValidationResponse response = new UniqueIDValidationResponse();
response.setResponse("Invalid request");
return Response.ok(response).build();
}
}
Bean class
public class UniqueIDValidationRequest {
/*#Autowired
private Validator validator;*/
#XmlAttribute(name = "ssoId")
#NotBlank
#NotEmpty
#Length(max=20,min=5)
protected String ssoId;
#NotEmpty
#NotBlank
#XmlAttribute(name = "epHashId")
protected String epHashId;
This issue is resolved now. What I had to do is create a custom interceptors. This will catch the fault occurred while bean validation and add actual bean validation error instead. Please check the InInterceptor code below -->
#Provider
public class IamServInInterceptor extends
BeanValidationInInterceptor implements ContainerRequestFilter{
private static Logger logger = LoggerFactory.getLogger(IamServInInterceptor.class);
public IamServInInterceptor() {
logger.debug("IamServInInterceptor default consturctor called");
}
public IamServInInterceptor(String phase) {
super(phase);
logger.debug("IamServInInterceptor phase : " + phase);
}
#Override
public void filter(ContainerRequestContext arg0) throws IOException {
// TODO Auto-generated method stub
logger.debug("IamServInInterceptor filter is called");
InterceptorChain chain = PhaseInterceptorChain.getCurrentMessage()
.getInterceptorChain();
chain.add(this);
}
protected Object getServiceObject(Message message) {
logger.debug("IamServInInterceptor getServiceObject called");
return ValidationUtils.getResourceInstance(message);
}
protected void handleValidation(Message message, Object resourceInstance,
Method method, List<Object> arguments){
try {
logger.debug("IamServInInterceptor handleValidation called");
super.handleValidation(message, resourceInstance, method, arguments);
} catch (ConstraintViolationException ex) {
logger.debug("IamServInInterceptor handleValidation has some exception");
UniqueIDValidationResponse response = new UniqueIDValidationResponse();
response.setResponse("INPUT_VALIDATION_FAILED");
ConstraintViolationException constraintException = (ConstraintViolationException)ex;
Set<ConstraintViolation<?>> constraint=
constraintException.getConstraintViolations();
Iterator<ConstraintViolation<?>> iterator = constraint.iterator();
while(iterator.hasNext()){
ConstraintViolation<?> error = iterator.next();
logger.debug("Error : " + error.toString());
response.getMessage().add(error.getMessage());
}
ex.printStackTrace();
message.getExchange().put(Response.class, Response.ok(response).build());
}
}
}
I resolved with a exception mapper:
import org.apache.cxf.validation.ResponseConstraintViolationException;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.Iterator;
/**
* Created by Hyun Woo Son on 1/9/18
**/
#Provider
public class ValidationExceptionMapperCustom implements ExceptionMapper<ValidationException> {
private static org.slf4j.Logger logger = LoggerFactory.getLogger(ValidationExceptionMapperCustom.class);
public Response toResponse(ValidationException exception) {
Response.Status errorStatus = Response.Status.INTERNAL_SERVER_ERROR;
ResponseVo responseVo = new ResponseVo();
if (exception instanceof ConstraintViolationException) {
ConstraintViolationException constraint = (ConstraintViolationException)exception;
Iterator i$ = constraint.getConstraintViolations().iterator();
StringBuilder errorMsg = new StringBuilder(RestExcepcionEnum.PARAMETROS_EXCEPCION.getValue());
errorMsg.append("\n");
while(i$.hasNext()) {
ConstraintViolation<?> violation = (ConstraintViolation)i$.next();
logger.info("error {}",violation.getRootBeanClass().getSimpleName() + "." + violation.getPropertyPath() + ": " + violation.getMessage());
errorMsg.append("La propiedad : "+violation.getPropertyPath() + " con error: " + violation.getMessage() );
errorMsg.append("\n");
}
if (!(constraint instanceof ResponseConstraintViolationException)) {
errorStatus = Response.Status.BAD_REQUEST;
}
responseVo.setMensaje(errorMsg.toString());
}
else{
responseVo.setMensaje("Error inesperado: "+exception.getMessage());
logger.debug("Error de validacion {}",exception.getMessage(),exception);
}
return Response.status(errorStatus).entity(responseVo).build();
}
}
And add it to the Application class:

Resources