使用角度$ http和Chrome的REST客户端进行POST会出现400 Bad request错误

时间:2015-07-09 22:02:04

标签: spring rest post get

我正在尝试在jboss上对我的spring控制器进行REST调用

@RequestMapping(value = "/myMethod", method = RequestMethod.POST)
public
@ResponseBody
String myMethod(@RequestBody MyClass myClass) {
    String json = null;
    String METHOD_NAME = "getAuditForRecordId";

    Gson gson = new GsonBuilder().create();
    try {
        json = gson.toJson("success");
    }

    return json;
}

我的角色呼叫

$http.post(<URL to myMethod>, postData,
            {
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
            }).success(function () {
                d.resolve(response);
            }).error(function () {
                d.reject();
            }
        );

因400 Bad请求错误而失败。我的jboss应用程序有一个CORSFilter

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
                     FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response=(HttpServletResponse) resp;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Accept, Content-Type");

    chain.doFilter(req, resp);
}

同一控制器内的GET方法工作正常。但是上面的POST失败了。

我的POST JSON对象有嵌套对象。

我也尝试过来自Chrome的REST客户端的同一个POST调用,但仍然会收到相同的错误。有人能指出我在这里犯的明显错误吗?

4 个答案:

答案 0 :(得分:0)

错误是帖子上的网址是在其他域中分配的,而不是在您的项目运行的同一个域中,您不会将URL称为其他域名。

这是我的例子:

@RequestMapping(value = "/listAll.json", params = {"page","pageSize","order","filter"},  method = RequestMethod.GET, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String listAll(@RequestParam("pageSize") int pageSize, @RequestParam("page") int page, @RequestParam("order") String order, @RequestParam("filter") String filters){ 
    return this.pilmotiformService.listAllIntermediario(pageSize, page, order, filters);        
}

@RequestMapping(value = "updateRecord.json", method = RequestMethod.POST, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String UpdateRecord(@RequestParam("cons") Long cons) throws Throwable{}

来自Angular:

myApp.service('myService', function($http, $rootScope, $routeParams) {          

this.oneFunction = function() {                     
    return $http({
        method: 'GET',
        url: WEB_SERVER+'/oneMethod.json',
        params: {modulo: 'FMT_FORMREGI,FMT_AUDITORIA,FMT_ESTADO,FMT_ADJUNTO,PIL_USUA,PIL_MOTIVO,PIL_MOTIFORM' }
     });
 } 

this.otherFunction = function(one, two, three) {        

    data = {one : one, two : two, three : three};

    return $http({
        method: 'POST',
        url: WEB_SERVER+'/otherMethod.json',
        data: data
     });
 }        

});

答案 1 :(得分:0)

Jackson mapper是将数据转换或序列化为实体的组件。

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-lgpl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-lgpl</artifactId>
            <version>1.9.13</version>
        </dependency>

答案 2 :(得分:0)

我不确定,但我认为你在这里缺少的是json采用这种格式

{"key" : "value"}

并且您正在尝试"success"所以可能这就是您所缺少的。

我可能错了,因为我没有在gson或json上工作很多

答案 3 :(得分:0)

I found the problem. It's in the data I am passing with my post request. I had one extra property in my passed-in nested Object. When I changed the data model on my server side to include that extra property my issue was resolved.