大佬教程收集整理的这篇文章主要介绍了在REST URL中传递JSON字符串,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我会做的是:@H_944_3@
httpentity<Object> entity = new httpentity<Object>(Json); // Create http Entity to post to rest endpoint
restTemplate.getmessageConverters().add(new MapPingJacksonhttpmessageConverter());
restTemplate.postForEntity(url, entity, Object.class);
在服务器端,您不需要以PathVariable的身份访问它,而是可以使用@H_944_3@
@requestMapPing(value = "/restresource", method=requestMethod.POST, consumes= "application/Json")
public @ResponseBody String useJson(@requestbody JsonObject object) {
因此,spring框架会自动将Json字符串转换为指定的对象类型。您可以相应地调整网址,然后重试。@H_944_3@
第一次使用REST并尝试将JSON字符串作为REST请求URL中的路径变量发送,我正在做的是从客户端发送请求:@H_944_3@
JSONObject json = new JSONObject();
try {
json.put("Test","123");
json.put("tracks","1234");
jj = json.toString();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = "http://localhost:8090/webApp/restresource/"+jj+".do";
st = (String) restTemplate.postForObject(url,jj,String.class,jj);
休息服务器:@H_944_3@
@requestMapping(value = "/restresource/{CodE}",method=requestMethod.POST,consumes= "application/json")
public @ResponseBody String useJson(@PathVariable String jj) {
String result;
JSONObject jObject = new JSONObject();
try {
result = jObject.getJSONObject(jj).toString();
} catch (JSONException jsE) {
jse.printStackTrace();
}
return result;
}
我得到的例外是 not enough variable values to expand "Test"
@H_944_3@
http://localhost:8090/app/restresource/%7B%22%22:%22123%22,%22tracks%22:%221234%22%7D.do
@H_944_3@
以上是大佬教程为你收集整理的在REST URL中传递JSON字符串全部内容,希望文章能够帮你解决在REST URL中传递JSON字符串所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。