Spring 3.2 Jackson2 (400 Bad request) - spring-mvc

I know spring 3.2 does convert a json to a list of objects with RequestBody annotation. Its not working for me. I can use regular Jackson object mapper to do it. Just checking if any one can help me.. Below is my json and controller method
[{"uniqueJqGridId":"1","fileProcessingDate":"2012-09-24","createdTimeStamp":"1348569180191","csoCode":"A-A ","cycleDate":"2012-09-24","accountDate":"2012-10-02","originName":"NCAA ","amount":"-95996.33","policyNumber":"C ","transactionCode":"PCH","id":"1"}]
#RequestMapping(method = RequestMethod.POST, value = "/washTransactions", headers="Content-Type=application/json")
public #ResponseBody RequestStatus washTransactions(#RequestBody List<ReconPolicy> policiesToWash)throws Exception{
reconciliationService.applyWashToTransactions(policiesToWash,getCurrentUser());
return new RequestStatus(true);
}

You're facing Java's Type Erasure problem. Spring is not able to pass the exact class type to the method so it's actually getting something like List<?> policiesToWash.
A workaround would be to create a class like
public class WashablePolishes extends ArrayList<ReconPolicy>
This way spring will retain the type through the super type chain.
or you could change your method to
public #ResponseBody RequestStatus washTransactions(#RequestBody ReconPolicy[] policiesToWash) throws Exception {...}

Thanks for you reply Varun. Starting from Spring 3.2,There is no type erasure problem. I found the issue after enabling spring debugging, I figure out it is failing on some unknown properties, I had to annotate my class with #JsonIgnoreProperties. Now it works.

Related

KafkaListener annotation at class level with errorhandler property ignored

When using the kafkalistener annotation at class level and the provided errorhandler property is ignored. When method is annotated with kafkalistner and the provided errorhandler is working. Is it expected behavior?
This is really bug. The piece of code:
String errorHandlerBeanName = resolveExpressionAsString(kafkaListener.errorHandler(), "errorHandler");
if (StringUtils.hasText(errorHandlerBeanName)) {
endpoint.setErrorHandler(this.beanFactory.getBean(errorHandlerBeanName, KafkaListenerErrorHandler.class));
}
Is missed in the:
private void processMultiMethodListeners(Collection<KafkaListener> classLevelListeners, List<Method> multiMethods,
Object bean, String beanName) {
Unfortunately I don't see a simple way to workaround this. Please, consider to have a single #KafkaListener method with an Object as payload and manual type routing in that method to others.
Feel free to raise a GitHub issue on the matter!

#PathVariable annotation used on a Map method argument

SpringMVC's official doc has the following line:
When a #PathVariable annotation is used on a Map argument, the map is populated with all URI template variables.
My understanding is that I can get a Map object with all path variables like this:
#RequestMapping(path = "/{year}/{month}/{day}")
public String getMap(#PathVariable Map<String, String> pathVariables) {
.....;
}
But I got error: "Could not find #PathVariable [pathVariables] in #RequestMapping" instead. Anyone knows why?
Which version of Spring do you use? Spring has supported this feature since 3.2.

provide query string request mapping variable at class level

I am trying to make spring boot application & swagger. Application is for REST service provide. I have made application running each page.
I have made a simple controller that have RequestMapping("/group/user/contact").
Which is working fine.
I am trying to do something like RequestMapping("/group/{type}/contact") at class level.
So my question is that is it possible ?
If yes then just want some basic guidance. and if no then fine.
My all request working fine. All request came from CORS filter class.
You can do this, the handler method should look something like
#Controller
#RequestMapping("/group/{type}/contact")
public class ClassLevelPathVariableController {
#ResponseBody
#RequestMapping(method = RequestMethod.GET)
public String classLevelMapping(#PathVariable String type) {
return type;
}
}
In this setup a GET request like e.g. /group/test/contact would be handled by the classLevelMapping method and the type variable will be populated with the value "test"

How to pass single parameter to contoller in Spring MVC?

I have some questions from a design point of view in Spring Web MVC.
Is it good practice to use Request Object in controller? If not, then what is alternative way to pass pass one text fields value to controller? Do I need to create one new from bean for this single fields?
It depends of the situation, in a few cases I used the HttpServletRequest; for example for writing a file to the output stream.
If you want to get the Request Parameters you can use the annotation #RequestParam, that it´s more easy to get the parameters from the request.
Depends that you want to handle, for example for a form you can use #ModelAttribute and this attribute can be in a session or in the request.
For example:
#Controller
public class YourController {
#RequestMapping(value = "someUrl", method = RequestMethod.GET)
public String someMethod(#RequestParam("someProperty") String myProperty)
{
// ... do some stuff
}
}
Check the documentation here:
#RequestParam
#ModelAttribute
#PathVariable

jsr-303 annotations on a spring mvc controller parameter

Is it possible to use Bean Validation annotations like #Past or #Length on Request Parameters in spring mvc?
I would like to do something like:
#RequestMapping(method = RequestMethod.POST)
public RedirectView initiateSignup(#RequestParam #Valid #Past Date birthdate, BindingResult birthdateResult,
HttpServletResponse httpServletResponse) throws HttpMediaTypeNotAcceptableException {
I made it work with the help of a blog post:
http://blog.codeleak.pl/2012/03/how-to-method-level-validation-in.html
an additional annotation and a bean post processor were nessecary, but now it works.
I don't think that's possible. You can apply #Valid but not e.g. #Past. You can instead create a model class with fields that correspond to your request parameters, and put the JSR-303 annotations on the class's fields. You can then use that class as the controller method argument type, with #Valid on it, and Spring should validate it appropriately.
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/validation.html#validation-mvc-jsr303

Resources