How to send and receive JSON data from a restful webservice using Jersey API

后端 未结 3 1847
孤城傲影
孤城傲影 2020-12-06 00:53
@Path(\"/hello\")
public class Hello {

    @POST
    @Path(\"{id}\")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public          


        
相关标签:
3条回答
  • 2020-12-06 01:16

    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

        <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>
    
    0 讨论(0)
  • 2020-12-06 01:25

    For me, parameter (JSONObject inputJsonObj) was not working. I am using jersey 2.* Hence I feel this is the

    java(Jax-rs) and Angular way

    I hope it's helpful to someone using JAVA Rest and AngularJS like me.

    @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
    }
    

    Client side I used 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;
        };
    
    0 讨论(0)
  • 2020-12-06 01:37

    Your use of @PathParam is incorrect. It does not follow these requirements as documented in the javadoc here. I believe you just want to POST the JSON entity. You can fix this in your resource method to accept JSON entity.

    @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));
    
    0 讨论(0)
提交回复
热议问题