Spring MVC @RequestBody JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20687086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC @RequestBody JSON
提问by Konstantin B.
I'm getting an error that the request sent by the client was syntactically incorrect. What is being done wrong? Here is my code:
我收到一个错误,表明客户端发送的请求在语法上不正确。做错了什么?这是我的代码:
@Entity
@Table(name = "display")
public class Display {
private String diagonal;
private String aspectRatio;
//getter and setter
}
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data:{'diagonal':"sss"}
})
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
@RequestMapping(value = "/updateDisplay", method = RequestMethod.POST)
public String updateDisplay(@RequestBody Display display){
System.out.print(display);
return null;
}
}
}
回答by Mike Rocke
I think you need to say what the service media type will consume for Spring to know how to unmarshall it. Probably application/json.
我认为您需要说明服务媒体类型将为 Spring 消耗什么才能知道如何解组它。大概application/json。
@RequestMapping(value = "/updateDisplay", method = {RequestMethod.POST},
consumes = {"application/json"})
Probably some Json library too, like Hymanson.
可能也有一些 Json 库,比如 Hymanson。
回答by dharam
Use the following:
使用以下内容:
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data:{"diagonal":"sss","aspectRatio":"0.5"},
contentType: 'application/json',
dataType: 'json',
})
it works.
有用。
EDIT
编辑
If you are booting up Spring application Context using annotaitons, then your config class must have:
如果您使用 annotaitons 启动 Spring 应用程序上下文,那么您的配置类必须具有:
@Override
protected void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(true)
.parameterName("mediaType").ignoreAcceptHeader(true)
.useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
And your ajax request must include
并且您的 ajax 请求必须包括
contentType: 'application/json',
dataType: 'json',
check the modified ajax call above.
检查上面修改后的ajax调用。
If you are booting up spring application context using XMLs then use the below:
如果您使用 XML 启动 spring 应用程序上下文,请使用以下内容:
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="mediaType" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
For more details on writing RESTFUL webservices with Spring 3.2 see my blog
有关使用 Spring 3.2 编写 RESTFUL Web 服务的更多详细信息,请参阅我的博客
回答by BurnetZhong
You must convert the JSON data to string before pass it to Spring MVC. So, here is the solution in your case:
在将 JSON 数据传递给 Spring MVC 之前,您必须将其转换为字符串。所以,这是您的情况的解决方案:
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data: JSON.stringify({'diagonal':"sss"})
})
回答by a better oliver
You don't need @RequestBody.
你不需要@RequestBody.
With @RequestBodySpring calls a converter that takes the whole request and converts it to an object of the required type. You send your data as application/x-www-form-urlencoded, which is the default of jQuery, and there is no built-in converter for that.
随着@RequestBody春天调用一个转换器,占用了整个请求,并将其转换为所需要的类型的对象。您将数据发送为application/x-www-form-urlencoded,这是 jQuery 的默认值,并且没有内置转换器。
Without @RequestBody, when you send form data, spring creates an empty object and sets the properties based on the data you sent. So in your case Spring would do something like
如果没有@RequestBody,当您发送表单数据时,spring 创建一个空对象并根据您发送的数据设置属性。所以在你的情况下,Spring 会做类似的事情
display = new Display();
display.setDiagonal("sss");
Which, I guess, is what you want.
我想,这就是你想要的。
回答by Daniela Morais
I don't know if this is your problem too, but with me the valueis wrong and caused a error 405, example:
我不知道这是否也是你的问题,但对我来说这value是错误的并导致了一个error 405,例如:
@RequestMapping(value = "/planilha/{id}", method = RequestMethod.PUT)
public String update(@PathVariable("id") String id, @RequestBody String jsonStr) {
BasicDBObject json = ((BasicDBObject) JSON.parse(jsonStr));
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject ola = dao.update(id, json);
return ola.toString();
}
@RequestMapping(value = "/planilha/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") String id) {
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject temp = dao.remove(id);
return temp.toString();
}
Needed the change for:
需要更改:
@RequestMapping(value = "/planilha/{id}/**", method = RequestMethod.PUT)
public String update(@PathVariable("id") String id, @RequestBody String jsonStr) {
BasicDBObject json = ((BasicDBObject) JSON.parse(jsonStr));
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject ola = dao.update(id, json);
return ola.toString();
}
@RequestMapping(value = "/planilha/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") String id) {
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject temp = dao.remove(id);
return temp.toString();
}

