java - Jersey 415 Unsupported Media Type -
i have been trying since hours correct http error 415 unsupported media type
still showing media unsupported page. adding headers application/json
in postman.
here 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 sent :" + input; jsonobject outputjsonobj = new jsonobject(); outputjsonobj.put("output", output); return outputjsonobj; } @get @produces(mediatype.text_plain) public string sayplaintexthello() { return "hello"; } }
here web.xml
file
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>lostlove</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>lostlove</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
how our objects serialized , deserialized , response stream , request stream, through messagebodywriter
s , messagebodyreader
s.
what happens search done registry of providers, 1 can handle jsonobject
, media type application/json
. if 1 can't found, jersey can't handle request , send out 415 unsupported media type. should exception logged on server side. not sure if gotten chance view log yet.
jersey doesn't have standard reader/writer org.json
objects. have search web implementation or write 1 yourself, register it. can read more how implement here.
alternatively, accept string , return string. construct jsonobject
string parameter, , call jsonobject.tostring()
when returning.
my suggestion instead use data binding framework jackson, can handle serializing , deserializing , out model objects (simple pojos). instance can have class like
public class model { private string input; public string getinput() { return input; } public void setinput(string input) { this.input = input; } }
you have model
method parameter
public returntype sayjsontexthello(model model)
same returntype
. create pojo type wan return. json properties based on javabean property names (getters/setters following naming convention shown above).
to support, can add maven dependency:
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>2.17</version> <!-- make sure jersey version matches 1 using --> </dependency>
or if not using maven, can see this post, jars can download independently.
some resources:
Comments
Post a Comment