我正在开发RESTful服务。 我无法将JSON数据发送到控制器。
我使用以下依赖项:
我也尝试过:
正如POSTing JSON to Spring MVC Controller Returns 400 Bad Request
所建议的那样我也输入了MessageConverters,
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
@Controller
@RequestMapping(value="/todo", consumes="application/json", produces="application/json")
//@RequestMapping(value="/todo", consumes="text/plain", produces="application/json")
public class TodoRestService {
Logger logger = LoggerFactory.getLogger(TodoService.class);
@Autowired
private TodoService todoService;
@RequestMapping(value="/{id}", method= RequestMethod.GET)
@ResponseBody public Todo find(@PathVariable("id") Long id) {
Todo todo = todoService.find(id);
return todo;
}
@RequestMapping(method=RequestMethod.POST)
@ResponseBody public Todo create(@RequestBody Todo todo) {
//public Todo create(@RequestBody String todo) {
//public Todo create(@RequestParam("todo") String todo) {
System.out.println(todo);
todoService.create(newTodo);
return newTodo;
}
}
public class TODO {
private String firstName;
private String lastName;
//getter setters
}
我正在使用邮递员来发布JSON
我已将Content-Type设置为application / json
表格数据----- name = todo value = {“firstName”:“nilam”,“lastName”:“naik”}
method = POST
我收到400 Bad Request。
但是如果我将用@RequestBody注释的参数的数据类型从TODO更改为String然后是 我得到了类似的东西,
------WebKitFormBoundaryu6banLlTPiPudsBB
Content-Disposition: form-data; name="todo"
{"firstName":"nilam","lastName":"naik"}
------WebKitFormBoundaryu6banLlTPiPudsBB--
我的杰克逊也在我的课堂上。
如果我将属性消耗从application / json更改为text / plain和
@RequestBody String todo然后我得到,
{"firstName":"nilam","lastName":"naik"}
但我不想手动将String转换为Java Object。但我不明白为什么@RequestBody无法将我的JSON数据转换为Java Object。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.orci</groupId>
<artifactId>OrciMavenTutorial</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>OrciMavenTutorial Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- <org.springframework.version>3.2.3.RELEASE</org.springframework.version> -->
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.2.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.5.3.0_1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.5.3.0_1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<!-- <version>1.9.6</version> -->
<!-- <version>1.4.2</version> -->
<!-- <version>1.9.9</version> -->
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<!-- <version>1.9.6</version> -->
<!-- <version>1.4.2</version> -->
<!-- <version>1.9.9</version> -->
<version>1.9.7</version>
</dependency>
</dependencies>
<build>
<finalName>OrciMavenTutorial</finalName>
</build>
请有人帮帮我。
答案 0 :(得分:5)
从你给出的一个解释清楚,你错误地使用POSTMAN。
您无法在@RequestBody
中发送form-data
带注释的对象。表单使用@ModelAttribute
或@RequestParam
(多个)进行处理。
如果要将JSON发布到Controller端点,则必须执行以下图像;
您必须明显选择POST
,并且必须将标题Content-Type
设置为application/json
,最后您必须从标签中选择raw
并从中选择JSON
落下。
如果您遵循此操作,您的Controller方法必须使用Todo
对象作为@RequestBody
注释,无需使用String
并手动转换为对象。