Ido_f Ido_f - 1 month ago 18
reST (reStructuredText) Question

Jersey Json and Pojo

I've spent far too many hours (over 10 i would say) trying to figure out how to get basic json calls (from angularjs) to open and process on my Jersey 2.4.
I've tried every possible result on google and am still getting a


415 (Unsupported Media Type)


client side and a


org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class jersey.webreadability.controllers.jsonmodels.TextInput, genericType=class jersey.webreadability.controllers.jsonmodels.TextInput.


on the server side.

I'll write here every possible file i've changed while trying to solve this, with the hope it will help someone to help me.
At the moment i don't really care how will it work as long as it will work, i do understand whoever that i should probably get this working with either Jackson or Gson.

Dependencies (from the POM File):

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17.1</version>
</dependency>


from the Web.xml:

<servlet>
<servlet-name>webReadability</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>jersey.webreadability.controllers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.algo.server.webservice;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>


The object class:

@XmlRootElement(name = "TextInput")
public class TextInput implements Serializable {

@XmlElement public String url;
@XmlElement public String text;
@XmlElement public String file;

public TextInput() {
}

public TextInput(String url, String text, String file) {
this.url = url;
this.text = text;
this.file = file;
}

@Override
public String toString() {
return "TextInput{" + "url=" + url + ", text=" + text + ", file=" + file +
'}';
}
}


main class:

@Path("/analysisController")
public class AnalysisController {

@POST
@Path("/sayHello")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String sayHello(final TextInput text) {
System.out.println("printing out the sent text: " + text);

return "hello " + text.file;
}
}


and js:

var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
{
url: "http://www.example.com",
text: "txext text tex",
file: $scope.textarea
}
]));

var httpRequest = $http({
method: 'POST',
url: '/rest/analysisController/sayHello/',
data: mockDataForThisTest,
headers: {
'Content-Type': 'application/json'
}
}).success(function(data, status) {
$scope.textarea = data;
console.log(data);
}).error(function(error) {
console.log('error!');
});


That's all I currently have, I hope it helps.

Thank you so very much.

Answer

I'm adding this as my own answer because i think this will help anyone with a horrible working copy of Jersey in the future. At the same time, the answer of @Michal Gajdos was grealy helpful and you should also relate to his text when having problems in the future.

What eventually helped me was a dependecy which includes pretty much every thing you need. simply add it and give it a try

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.2.3</version>
</dependency>

source: Git

Comments