Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration of Spring from 3.2 to 4.1.1: trouble with JSON serialization

I recently migrate our project from Spring 3 to Spring 4.1.1. I also migrate jackson from version 1 to version 2.3.0.

Now, I encounter problems when using controllers with void response

@RequestMapping(value="toto", method="POST")
public @ResponseBody void myController() {
//content
}

In runtime, when calling it I get an exception of this form:

Failed to evaluate serialization for type [void]: java.lang.IllegalStateException: Failed to instantiate standard serializer (of type com.fasterxml.jackson.databind.ser.std.NullSerializer): Class com.fasterxml.jackson.databind.ser.BasicSerializerFactory can not access a member of class com.fasterxml.jackson.databind.ser.std.NullSerializer with modifiers "private"

I wonder if someone encountered the same kind of issue or have an idea of what is wrong.

Thanks in advance.

like image 403
user3629050 Avatar asked Nov 03 '14 16:11

user3629050


2 Answers

If you want to use a void return type you need to annotate the method with @ResponseStatus(value = HttpStatus.OK):

@RequestMapping(value = "/usage")
@ResponseStatus(value = HttpStatus.OK)
public void doSomething(HttpServletRequest request, ...

For details see What to return if Spring MVC controller method doesn't return value?

like image 183
Holger Brandl Avatar answered Nov 13 '22 05:11

Holger Brandl


Your method is not returning anything, when Spring is waiting for a return value to serialize using HttpMessageConverters. You should rather have something like this:

@RequestMapping(value="toto", method="POST")
@ResponseBody
public FooBar myController() {
  // 
  return fooBar;
}
like image 1
Brian Clozel Avatar answered Nov 13 '22 05:11

Brian Clozel