1
我想从GWT发布JSON消息到Spring MVC框架中的控制器。 spring控制器正在接收来自GWT的请求,但是当我输出请求内容长度和request.toString()时,它显示内容长度为0,并且request.toString()的输出没有任何内容。从GWT到Spring MVC控制器的JSON消息发布
以下是我的代码和输出:
以下是执行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;
}
以下是从上面打印出来的输出:从上面的输出可以看到JSON消息
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消息发布和接收的方式?
您好nDijax,感谢您的帮助。我尝试了这两个帖子,并获得方法。 get方法工作得很好,但post方法不起作用。 Witht post方法我得到的响应状态代码错误400,我想这是由于控制器没有收到任何带有“jsonParam”参数的内容,或者我应该说从post方法中根本没有内容。看起来像“builder.setRequestData(”jsonParam =“+ new JSONObject(jso).toString());方法的错误”;“那么你认为nDijax? – Sundaynjan
你可以登录请求发布数据吗? – nDijax
是的,我可以在GWT客户端打印出请求数据的内容。在发布到服务器端(控制器)之后,请求内容是空的,但内容类型在那里。 – Sundaynjan