简体   繁体   中英

Getting the different Content Type in the response of REST call done by REST client and Jersey Java Code

While working on a Micro-Service, I have to hit the REST api of the 3rd party. I am using the Spring Boot Application with Jersey library. Now the problem is that I am getting the content type of the response as "text/html; charset=utf-8".

If I hit the same call using the REST client, I get the right content type as application/json;charset=UTF-8. Why so ?

Below is the Java source code for the same -

@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")
@POST
@Path("/endPoint")
@Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")
public JSONObject getAccessToken(@FormParam("item1") String item1,@FormParam("item2") String item2,@FormParam("item3") String item3,@FormParam("item4") String item4) throws Exception {
  System.out.println("Enter to test");


    String extendedUrl = "?item1="+item1+"&item2="+item2+"&item3="+item3+"&item4="+item4;

    JSONObject jObject = null;
    try {
      jObject = postCall(extendedUrl);
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    System.out.println("Box Auth Response :: "+jObject.toJSONString());

    return jObject;
}
// Short description of the logic to execute the request
public void postCall(String extendedUrl)
{ 
String url = "baseurl";
url+=extendedUrl;
HttpsURLConnection conn = openConnection(apiUrl);
conn.connect();
status = conn.getResponseCode();
String responseContentType = conn.getContentType();
System.out.println("responseContentType ::"+responseContentType);
}

So when I debug the code, responseContentType comes out as text/html; charset=utf-8. Is there any reason for the same ? How will get this as application/json;charset=UTF-8?

Help would be appreciated.

Check "accept" header of your request

Accept: application/json Content-Type: application/json

该信息位于HEADERS - CONTENT TYPE中

Actually I was hitting some different end point which was not the part of OAuth in turn I was getting the HTML response. Issue has been solved.Thanks guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM