简体   繁体   English

Spring MVC-RequestBody不起作用(JSON)

[英]Spring MVC - RequestBody not working (JSON)

I'm having a simple controller method: 我有一个简单的控制器方法:

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public void postCheck(@RequestBody TestDTO testDTO, HttpServletRequest request) {

System.out.println(testDTO);

}

the DTO is: DTO是:

public class TestDTO {

private String hello;

public TestDTO() {

}

public String getHello() {
    return hello;
}

public void setHello(String hello) {
    this.hello = hello;
}

} }

When i try to post a request to that url i get an ERROR 400 Message response "The request sent by the client was syntactically incorrect." 当我尝试将请求发布到该URL时,出现错误400消息响应“客户端发送的请求在语法上不正确。” (the controller method is not being invoked at all) (完全不调用controller方法)

I tried posting from Chrome's poster plugin AND from jquery's ajax: 我尝试从Chrome的海报插件和jquery的ajax发布:

$.ajax({
    type : "POST",
    url : "http://localhost:8080/NG_Admin/test",
    contentType : "application/json",
    dataType : "json",      
    data : JSON.stringify(eval({"hello" : "world"})),
    error : function(data) {
        alert('Error');
    },
    success : function(data) {  
            alert('Success!');
    }
});
}

Any ideas? 有任何想法吗? (btw I use both 'jackson-mapper-asl', version:'1.9.12', 'jackson-core-asl', version:'1.9.12') (顺便说一句,我同时使用了'jackson-mapper-asl'版本:'1.9.12','jackson-core-asl'版本:'1.9.12')

Thanks 谢谢

OK,I fixed the problem, 好,我解决了这个问题,

Had to add a filter for all request that handles CORS access. 必须为处理CORS访问的所有请求添加过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM