从GWT到Spring MVC控制器的JSON消息发布



我正在尝试将来自GWT的JSON消息发布到Spring MVC Framework中的控制器。Spring 控制器正在接收来自 GWT 的请求,但是当我打印出请求内容长度和 request.toString() 时,它显示内容长度为 0,并且 request.toString() 的输出没有任何内容。

以下是我的代码和输出:

以下是我执行JSON消息发布的GWT代码:

@UiHandler("saveButton")
void onClick(ClickEvent e) {
    UserAddJSO jso = (UserAddJSO)JavaScriptObject.createObject().cast();
    jso.setFirstName("Alex");
    jso.setLastName("Cheng");
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "addUser.aj");
    builder.setHeader("Content-Type", "application/json");
    builder.setRequestData(new JSONObject(jso).toString());
    System.out.println("request data :"+builder.getRequestData());
    try
    {
        builder.sendRequest(null, new RequestCallback()
        {
            public void onError(Request request, Throwable exception)
            {
                Window.alert("on error");
            }
            public void onResponseReceived(Request request, Response response)
            {
                Window.alert("response received :"+response.getStatusCode());
                Window.alert(response.getText());
            }
        });
    }
    catch (RequestException excp)
    {
        Window.alert("exception catched");
    }
}

从上面的代码中,您可以看到我在请求数据上打印出来,JSON字符串就在那里。

以下是我的弹簧控制器:

@RequestMapping(value = "**/addUser.aj", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public List<UserAddDTO> getPostStocks(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    List<UserAddDTO> userDTO = new ArrayList<UserAddDTO>();
    JSONObject jObj;
    jObj = new JSONObject(request.getParameterMap());
    System.out.println(jObj.toString());
    System.out.println("xxxxxxxxxxxxx "+request.toString());
    System.out.println("content type :"+request.getContentType());
    System.out.println("inside post method");
    return userDTO;
}

以下是以上打印输出的输出:

request data :{"firstName":"Alex", "lastName":"Cheng"}
content type :application/json
content length :0
inside post method
{}
xxxxxxxxxxxxx POST /addUser.aj HTTP/1.1
Accept: */*
Referer: http://127.0.0.1:8888/AlliumApp.html?gwt.codesvr=127.0.0.1:9997
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; Tablet PC 2.0; BRI/2)
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:8888
Connection: Keep-Alive
Content-Type: application/json
Content-Length: 0
Cache-Control: no-cache

从上面的输出中,您可以看到请求中存在 JSON 消息。但是当它到达控制器时,JSON 消息不再存在。那么我是否以正确的方式发布和接收 json 消息?

试试这个:

开机自检方法

builder.setRequestData("jsonParam=" + new JSONObject(jso).toString());

控制器:

@RequestMapping(value = "**/addUser.aj", method = RequestMethod.POST, produces =       "application/json")
@ResponseBody
public List<UserAddDTO> getPostStocks(HttpServletRequest request, HttpServletResponse response, @RequestParam("jsonParam") String jsonParam) {
    ...
    JSONObject jObj;
    jObj = new JSONObject(jsonParam);
}

获取方法:

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
                              "addUser.aj?jsonParam=" + new JSONObject(jso).toString());

在控制器中:

@RequestMapping(value = "**/addUser.aj", method = RequestMethod.GET, produces =   "application/json")
@ResponseBody
public List<UserAddDTO> getPostStocks(HttpServletRequest request, HttpServletResponse response, @RequestParam("jsonParam") String jsonParam) {
    ...
    JSONObject jObj;
    jObj = new JSONObject(jsonParam);
}

最新更新