Jersey JSON Response as HttpUrlConnector instead of JSON Jersey JSON Response as HttpUrlConnector instead of JSON json json

Jersey JSON Response as HttpUrlConnector instead of JSON


Instead of response.getEntity(), use response.readEntity(String.class) or response.readEntity(HttpEntity.class).


Client Processing code

package net.test;import java.util.HashMap;import org.codehaus.jettison.json.JSONException;import org.codehaus.jettison.json.JSONObject;import com.sun.jersey.api.client.Client;import com.sun.jersey.api.client.ClientResponse;import com.sun.jersey.api.client.WebResource;public class Consumer {    public static void main(String[] args) {        Client client = Client.create();        ClientResponse clientResponse = null;        User u = new User();        u.setUsername("hello");        u.setPassword("hello");        WebResource webResource = client            .resource("http://localhost:8080/"+appName"+/logins");        HashMap<String, String> userMap = new HashMap<String, String>();        userMap.put("username", u.getUsername());        userMap.put("password", u.getPassword());        // JSON logins request!        JSONObject jsonLoginRequest = new JSONObject();        try {            jsonLoginRequest.put("login-request", userMap);            System.out.println(jsonLoginRequest.get("login-request"));            System.out.println(jsonLoginRequest.getJSONObject("login-request")                .get("username"));        } catch (JSONException e) {            e.printStackTrace();        }       // POST Operation       clientResponse = webResource.post(ClientResponse.class,            jsonLoginRequest);       System.out.println(jsonLoginRequest);    // Send the return value in object       String result = clientResponse.getEntity(String.class);       System.out.println(result); }}

Server Processing Code

package net.test;//Server Processing codeimport java.util.HashMap;import java.util.Random;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.Context;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import org.codehaus.jettison.json.JSONException;import org.codehaus.jettison.json.JSONObject;@Path("/")public class Producer {   @POST   @Path("logins")   @Consumes(MediaType.APPLICATION_JSON)   @Produces(MediaType.APPLICATION_JSON)   public Response generateToken(String objInputData) {    JSONObject jsonObject = null;    User user = null;    String username = null;    String password = null;    try {        jsonObject = new JSONObject(objInputData);        user = new User();        username = jsonObject.getJSONObject("login-request")                .get("username").toString();        password = jsonObject.getJSONObject("login-request")                .get("password").toString();        // Your business logic goes here        // DBHandshaker handshaker = DBHandshaker.getInstance();        // user = handshaker.logUser(username, password);        System.out.println(username+password);        if (user != null) {            // write your logic to generate token            // StringUtil stringUtil = StringUtil.getInstance();            // String tokenString =            // stringUtil.encryptForToken(user.getUsername(),            // user.getPassword());            // dummy string to test            String tokenString = "Helllllllllgjdkg";            HashMap<String, String> tokenMap = new HashMap<String, String>();            tokenMap.put("token", tokenString);            JSONObject jsonLoginResponse = new JSONObject();            try {                jsonLoginResponse.put("login-token", tokenMap);            } catch (JSONException e) {                e.printStackTrace();            }            return Response.ok(jsonLoginResponse.toString(),                    MediaType.APPLICATION_JSON).build();        } else {            return Response.status(Response.Status.UNAUTHORIZED).build();        }    } catch (Exception e1) {        return Response.status(Response.Status.NO_CONTENT).build();    }}}

Write catcher logic after getting the response

hope this answer is helpful for you