Dhana
Dhana

Reputation: 563

Passing Json to Rest and receiving it through JSONObject

I am having the following server side code:-

    import org.json.JSONObject;
    @Path("/user")
    public class Users {

    @POST
    @Path("register")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response registerUser(JSONObject userDetails) {
        return Response.status(Status.ACCEPTED).entity("User Created.Details are: " + userDetails).build();
    }
}

Once I tried to call this using the following I am getting a 415 error. Can you please let me know what is the way to solve this.

{
    "user_id": "[email protected]",
    "password": "mashakawa",

    "user_profile": {
        "name": "Masha",
        "city": "New York",
        "email": "[email protected]",
        "age": 20
    },

    "user_settings": {
        "phone_number": "+91898342123"
    }
}

By the way i am using Jersey.

Upvotes: 2

Views: 2944

Answers (2)

Srijani Ghosh
Srijani Ghosh

Reputation: 4216

A simple way to do that is --

public void parseJson(WebResource service) throws UniformInterfaceException, JSONException {

  JSONArray resultArray = new JSONArray(service.path("resources/test/json/Tom").get(String.class));

  List<User> userList = new ArrayList<User>();

  for (int count = 0; count < resultArray.length(); count++) {

    JSONObject userObject = resultArray.getJSONObject(count);
    // this is tor read a sample JSON., this code will change according to your need.
    User user = new User(userObject.get("name").toString(), userObject.get("surname").toString(), userObject.get("address").toString()); 

    userList.add(user);

  }

  System.out.println(userList);

 }

This code is written specifically for Jersey web services.

Please find more links for coding in Jersey -

Creating Webservices with Maven and Jersy

Edit : (Client needs java-json.jar in it's classpath to develop this code).

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

In JAX-RS, you will have to create a JSON MessageBodyReader that will readFrom InputStream and return a JSONObject.

@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class JSONObjectMessageBodyReader implements MessageBodyReader<JSONObject> {

    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public JSONObject readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException {
        //Using entityStream, read the content and return a  JSONObject back...
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(entityStream, "UTF-8")); 
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        return new JSONObject(responseStrBuilder.toString());
    }
}

Upvotes: 2

Related Questions