Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 759
Ido_f Avatar asked Nov 14 '13 00:11

Ido_f


2 Answers

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

like image 105
Ido_f Avatar answered Oct 03 '22 15:10

Ido_f


Dependencies

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.

web.xml

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).

Register features

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.

like image 35
Michal Gajdos Avatar answered Oct 03 '22 16:10

Michal Gajdos