问题描述
我正在开发 RESTful 服务.我无法将 JSON 数据发布到控制器.
I am developing RESTful services.I am not able to POST JSON data to controller.
我正在使用以下依赖项:
I am using the following dependencies:
- 春季:3.2.3.RELEASE
- 杰克逊:1.9.6
我也试过:
- Spring 3.2.3.RELEASE 和 Jackson 1.9.9
- Spring 3.1.1.RELEASE 和 Jackson 1.9.9
如这里所建议的 将 JSON 发布到 Spring MVC 控制器返回 400 个错误请求
我也输入了 MessageConverters,
I made entries of MessageConverters also,
<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;
}
}
POJO
public class TODO {
private String firstName;
private String lastName;
//getter setters
}
我正在使用邮递员来发布 JSON
I am using postman to POST the JSON
我已将 Content-Type 设置为 application/json
I have set Content-Type as application/json
表单数据-----名称 = 待办事项value = {"firstName":"nilam","lastName":"naik"}
form data -----name = todovalue = {"firstName":"nilam","lastName":"naik"}
方法 = POST
我收到 400 个错误请求.
I am getting 400 Bad Request.
但是如果我将使用@RequestBody 注释的参数的数据类型从 TODO 更改为 String 那么
我得到了类似的东西,
But If I changed data type of parameter annotated with @RequestBody from TODO to String then
I am getting something like,
------WebKitFormBoundaryu6banLlTPiPudsBB
Content-Disposition: form-data; name="todo"
{"firstName":"nilam","lastName":"naik"}
------WebKitFormBoundaryu6banLlTPiPudsBB--
我的类路径上也有 Jackson.
I also have Jackson on my classpath.
如果我将属性消耗从 application/json 更改为 text/plain 和
If I changed attribute consumes from application/json to text/plain and
@RequestBody String todo 然后我得到,
@RequestBody String todo then I get,
{"firstName":"nilam","lastName":"naik"}
但我不想手动将字符串转换为 Java 对象.但我不明白为什么@RequestBody 无法将我的 JSON 数据转换为 Java 对象.
But I don't want to manually convert the String to Java Object. But I don't understand why @RequestBody is not able to convert my JSON data to 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>
请有人帮助我.
推荐答案
从你给出的解释中可以清楚地看出,你错误地使用了 POSTMAN.
From the explanation you have given one thing is clear, You are using POSTMAN wrongly.
您不能在 form-data
中发送带有 @RequestBody
注释的对象.使用 @ModelAttribute
或 @RequestParam
(多个)处理表单.
You can not send a @RequestBody
annotated object in a form-data
. Forms are handled using @ModelAttribute
or @RequestParam
(Multiple of this).
如果您想将 JSON 发布到控制器端点,您必须像下图那样操作;
If you want to post a JSON to an Controller end point you must do like the following image;
显然你必须选择POST
并且必须设置一个标题Content-Type
为application/json
,最后你必须选择raw
从选项卡中选择 JSON
从下拉列表中.
You must select POST
obviously and must set a header Content-Type
as application/json
, finally you must select raw
from the tabs and select JSON
from the dropdown.
如果你遵循这个你的控制器方法必须使用 Todo
对象作为 @RequestBody
注释并且不需要使用 String
并手动转换为对象.
If you follow this your Controller method must work with Todo
object as @RequestBody
annotation and no need to use String
and manually convert to object.
这篇关于将 JSON 发布到控制器返回 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!