I use IDEA to build a simple RESTful server based on SpringMVC. But @RestController and @ResponseBody can't convert POJO to JSON by jackson JSON.
In dispatcher-servlet.xml :
<mvc:annotation-driven/>
In pom.xml :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
My controller:
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/one")
public One getOne() {
return new One(1);
}
}
My POJO:
public class One {
public One(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
private int number;
}
But I still get the No converter found for return value of type error. I don't know why. Have I met some config or something else?
Try putting this in your dispatcher-servlet.xml:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
You need to specify produces/consumes to define which type of data type you want, for example:
@RestController
@RequestMapping("/test", produces = {MediaType.APPLICATION_JSON_VALUE})
public class TestController {
@GetMapping("/one")
public One getOne() {
return new One(1);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.