[英]How to send and receive JSON data from a restful webservice using Jersey API
@Path("/hello")
public class Hello {
@POST
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject sayPlainTextHello(@PathParam("id")JSONObject inputJsonObj) {
String input = (String) inputJsonObj.get("input");
String output="The input you sent is :"+input;
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj;
}
}
This is my webservice (I am using Jersey API). 这是我的webservice(我正在使用Jersey API)。 But I could not figure out a way to call this method from a java rest client to send and receive the json data.
但我无法找到一种方法从java rest客户端调用此方法来发送和接收json数据。 I tried the following way to write the client
我尝试了以下方式来编写客户端
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
JSONObject inputJsonObj = new JSONObject();
inputJsonObj.put("input", "Value");
System.out.println(service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).entity(inputJsonObj).post(JSONObject.class,JSONObject.class));
But this shows the following error 但这显示以下错误
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.lang.Class, and MIME media type, application/octet-stream, was not found
Your use of @PathParam is incorrect. 您对@PathParam的使用不正确。 It does not follow these requirements as documented in the javadoc here .
它不遵循这里的javadoc中记录的这些要求。 I believe you just want to POST the JSON entity.
我相信你只想发布JSON实体。 You can fix this in your resource method to accept JSON entity.
您可以在资源方法中修复此问题以接受JSON实体。
@Path("/hello")
public class Hello {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject sayPlainTextHello(JSONObject inputJsonObj) throws Exception {
String input = (String) inputJsonObj.get("input");
String output = "The input you sent is :" + input;
JSONObject outputJsonObj = new JSONObject();
outputJsonObj.put("output", output);
return outputJsonObj;
}
}
And, your client code should look like this: 而且,您的客户端代码应如下所示:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource(getBaseURI());
JSONObject inputJsonObj = new JSONObject();
inputJsonObj.put("input", "Value");
System.out.println(service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).post(JSONObject.class, inputJsonObj));
For me, parameter (JSONObject inputJsonObj) was not working. 对我来说,参数(JSONObject inputJsonObj)无效。 I am using jersey 2.* Hence I feel this is the
我正在使用球衣2. *因此我觉得这是
factory.update = function () {
data = {user:'Shreedhar Bhat',address:[{houseNo:105},{city:'Bengaluru'}]};
data= JSON.stringify(data);//Convert object to string
var d = $q.defer();
$http({
method: 'POST',
url: 'REST/webApp/update',
headers: {'Content-Type': 'text/plain'},
data:data
})
.success(function (response) {
d.resolve(response);
})
.error(function (response) {
d.reject(response);
});
return d.promise;
};
Client side I used AngularJS 客户端我使用AngularJS
factory.update = function () { data = {user:'Shreedhar Bhat',address:[{houseNo:105},{city:'Bengaluru'}]}; data= JSON.stringify(data);//Convert object to string var d = $q.defer(); $http({ method: 'POST', url: 'REST/webApp/update', headers: {'Content-Type': 'text/plain'}, data:data }) .success(function (response) { d.resolve(response); }) .error(function (response) { d.reject(response); }); return d.promise; };
The above problem can be solved by adding the following dependencies in your project, as i was facing the same problem.For more detail answer to this solution please refer link SEVERE:MessageBodyWriter not found for media type=application/xml type=class java.util.HashMap 上述问题可以通过在项目中添加以下依赖项来解决,因为我遇到了同样的问题。有关此解决方案的更多详细信息,请参阅链接SEVERE:找不到媒体类型= application / xml type = class java的MessageBodyWriter。 util.HashMap
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.