Jersey 415 Unsupported Media Type

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

I have been trying since hours to correct http error 415 Unsupported Media Type but it is still showing media unsupported page. I am adding headers application/json in Postman.

Here is my Java Code

package lostLove;  import javax.ws.rs.Consumes; import javax.ws.rs.GET;   import javax.ws.rs.POST; import javax.ws.rs.Path;   import javax.ws.rs.PathParam;   import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response;   import org.json.JSONObject;   @Path("/Story")  public class Story {        @POST       @Consumes({"application/json"})       @Produces(MediaType.APPLICATION_JSON)     //  @Consumes(MediaType.APPLICATION_JSON)     //  @Path("/Story")        public JSONObject sayJsonTextHello(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;       }        @GET         @Produces(MediaType.TEXT_PLAIN)          public String sayPlainTextHello() {           return "hello";       }  } 

here is my web.xml file

LostLoveindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspJersey REST Serviceorg.glassfish.jersey.servlet.ServletContainerjersey.config.server.provider.packageslostLove1Jersey REST Service/rest/*

回答1:

How our objects are serialized and deserialized to and from the response stream and request stream, is through MessageBodyWriters and MessageBodyReaders.

What will happens is that a search will be done from the registry of providers, for one that can handle JSONObject and media type application/json. If one can't be found, then Jersey can't handle the request and will send out a 415 Unsupported Media Type. You should normally get an exception logged also on the server side. Not sure if you gotten a chance to view the log yet.

Jersey doesn't have any standard reader/writer for the org.json objects. You would have to search the web for an implementation or write one up yourself, then register it. You can read more about how to implement it here.

Alternatively, you could accept a String and return a String. Just construct the JSONObject with the string parameter, and call JSONObject.toString() when returning.

My suggestion instead would be to use a Data binding framework like Jackson, which can handle serializing and deserializing to and from out model objects (simple POJOs). For instance you can have a class like

public class Model {     private String input;     public String getInput() { return input; }     public void setInput(String input) { this.input = input; } }  

You could have the Model as a method parameter

public ReturnType sayJsonTextHello(Model model) 

Same for the ReturnType. Just create a POJO for the type you wan to return. The JSON properties are based on the JavaBean property names (getters/setters following the naming convention shown above).

To get this support, you can add this Maven dependency:

org.glassfish.jersey.mediajersey-media-json-jackson2.17

Or if you are not using Maven, you can see this post, for the jars you can download independently.

Some resources:



回答2:

Its because of following issue:

JAX-RS does not support default Jackson mapping conversion. So if you have the ajax request as below(Post):

 jQuery.ajax({            url: "http://localhost:8081/EmailAutomated/rest/service/save",             type: "POST",             dataType: "JSON",             contentType: "application/JSON",             data: JSON.stringify(data),             cache: false,             context: this,             success: function(resp){                    // we have the response                    alert("Server said123:\n '" + resp.name + "'");                  },                  error: function(e){                    alert('Error121212: ' + e);                  }           }); 

and in JAX-RS controller side you need to do like below:

@Path("/save") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String saveDetailsUser(String userStr) {      Gson gson = new Gson();     UserDetailDTO userDetailDTO = gson.fromJson(userStr, UserDetailDTO.class);      String vemail = userDetailDTO.getEMAIL();      return "userDetailDTO"; } 

Here please make sure on parameter. service is accepting json as String not the POJO.

Surely It will work. Thanks!



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!