I am currently trying to create my first rest service with spring , I wan't him to return XML result (based on JAXB) .
on a very basic rest controller:
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
public class CVIController {
@RequestMapping(value = "/resume")
public @ResponseBody
Integer getResume() {
return 5;
}
}
with a spring-servlet.xml pretty empty:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="fr.urouen"
/>
<mvc:annotation-driven />
</beans>
And a Tomcat server under JDK8 so I can be sure that all modules are load by default on the JVM... I got the following exception throwed:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.lang.Integer
I don't wan't to use Jackson because I know that JAXB can handle this. But after juggle with some part of my code, I still can't found the answer of why she is throwed.
Edit 1:
I tried to create a dummy class with getters as answers said
public class Dummy {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@RestController
public class CVIController {
@RequestMapping(value = "/resume")
public ResponseEntity<Dummy> getResume() {
Dummy dummy = new Dummy();
dummy.setValue(5);
return new ResponseEntity<Dummy>(dummy, HttpStatus.OK);
}
}
But still got the exact same result :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class fr.urouen.model.Dummy
The simplest solution is to return a String and remove the @ResponseBody annotation. As a rest controller, by definition, returns a string, that's the way to go for this specific example.
If you are after a more comprehensive solution, Spring by default uses Jackson to do the conversion from object to JSON. The @ResponseBody does that automatically.
Im pretty sure that Integer won't work because it's not a POJO with getters and setters and something that can be converted to JSON. ie { 5 } is not a valid JSON object.
if you had a class like this:
public class returnObject() {
private int value;
... getters/setters
}
and then did this:
@RestController
@ResponseBody
public ReturnObject getValue() {
ReturnObject ret = new ReturnObject();
ret.setValue(5);
return ret;
}
You would then get this:
{ "value" : 5 }
You will also need to add jackson to your pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
ResponseEntity is used for a return some content from restcontroller, Have you tried with ResponseEntity
?
@RestController
public class CVIController {
@RequestMapping(value = "/resume")
public @ResponseBody ResponseEntity<?> getResume() {
return ResponseEntity.ok(5);
}
}
or you can write
@RestController
public class CVIController {
@RequestMapping(value = "/resume")
public @ResponseBody ResponseEntity<Integer> getResume() {
return new ResponseEntity<Integer>(5, HttpStatus.OK);
}
}
more example you can get from here
我也遇到了这个问题,尝试下载jackson-databind.jar、ackson-core.jar、ackson-annotations.jar,放到你的WEB-INF/lib目录下
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.