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.
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
Remove both jersey-media-json
(this module doesn't exist any more in Jersey 2.x) and jersey-json
(which is only for Jersey 1.x) and add one of the modules jersey-media-moxy
(JAXB) or jersey-media-json-jackson
(POJO). See JSON chapter in Jersey User Guide for more information on these modules.
Remove com.sun.jersey.config.property.packages
and com.sun.jersey.api.json.POJOMappingFeature
which are Jersey 1.x specific and have no use in Jersey 2.x. The first one has been replaced with jersey.config.server.provider.packages
and the second one has been removed (to use POJO JSON<->Object mapping use Jackson module jersey-media-json-jackson
).
If you're using jersey-media-json-jackson
module you need to register JacksonFeature in an extension of JAX-RS application or in web.xml
(using properties from ServerProperties) - see Jackson chapter. MOXy (jersey-media-moxy
) is registered automatically when the module is on the class-path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With