![](/img/trans.png)
[英]Android :-Consume a web service through Post method as a json Request and Response
[英]Java Jersey Web Service: consume JSON request
我有一个用 Java 和 Jersey 制作的网络服务。 我想接收一个 JSON 请求并解析 json 以将存储在 json 上的值保存在数据库中。
这是小米网络服务代码:
@Path("companies")
public class Companies {
@Path("add")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject addCompanies(JSONObject inputJsonObj){
String input = (String) inputJsonObj.get("company");
String output = "The input you sent is :" + input;
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj;
}
}
客户端是用 AngularJS 制作的:
$scope.company = "";
$scope.submit = function(){
// Writing it to the server
//
var dataObj = {
company : $scope.company
};
var res = $http.post('http://localhost:8080/WS-Test2/crunchify/companies/add', dataObj);
res.success(function(data, status, headers, config) {
$scope.message = data;
notify("succes");
});
res.error(function(data, status, headers, config) {
//alert( "failure message: " + JSON.stringify({data: data}));
notify("fail");
});
};
这是我将 JSON 传递给 Web 服务时遇到的错误:
Status Code:415
这是我发送的请求:
{"company":"Testing2"}
这是我的网络标签:
如果没有进一步的配置, JSONObject
不是 Jersey 支持的。 你只需要使用字符串
public String addCompanies(String json){
JSONObject inputJsonObj = new JSONObject(json)
String input = (String) inputJsonObj.get("company");
String output = "The input you sent is :" + input;
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj.toString();
}
如果你真的想使用JSONObject
你可以查看这篇文章。
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.