首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Jersey API从send风格的API服务发送和接收JSON数据

如何使用Jersey API从send风格的API服务发送和接收JSON数据
EN

Stack Overflow用户
提问于 2013-01-30 17:27:26
回答 3查看 160.7K关注 0票数 17
代码语言:javascript
复制
@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;
    }
} 

这是我的API服务(我正在使用Jersey API)。但是我想不出从java rest客户端调用这个方法来发送和接收json数据的方法。我尝试了以下方法来编写客户端

代码语言:javascript
复制
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));

但这显示了以下错误

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-31 16:24:35

您对@PathParam的使用不正确。它不遵循javadoc here中记录的这些要求。我相信您只是想发布JSON实体。您可以在资源方法中修复此问题,以接受JSON实体。

代码语言:javascript
复制
@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;
  }
}

而且,您的客户端代码应该如下所示:

代码语言:javascript
复制
  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));
票数 22
EN

Stack Overflow用户

发布于 2017-06-23 22:00:01

对我来说,参数(JSONObject inputJsonObj)不起作用。我正在使用jersey 2。*因此我觉得这是

java(Jax-rs)和Angular方式

我希望它对像我这样使用JAVA Rest和AngularJS的人有帮助。

代码语言:javascript
复制
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> methodName(String data) throws Exception {
    JSONObject recoData = new JSONObject(data);
    //Do whatever with json object
}

客户端我用的是AngularJS

代码语言:javascript
复制
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;
    };
票数 3
EN

Stack Overflow用户

发布于 2018-12-06 14:04:00

上述问题可以通过在您的项目中添加以下依赖项来解决,因为我面对的是相同的problem.For更详细的解决方案请参阅链接SEVERE:MessageBodyWriter not found for media type=application/xml type=class java.util.HashMap

代码语言:javascript
复制
    <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>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14600510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档