p.magalhaes
p.magalhaes

Reputation: 8364

Jersey + Jetty + JSON

I would like to produce an JSON using Jetty + Jersey. My POM.XML is similar to this post: How do I update example to work with latest versions Jetty (9.1.0.RC2) and Jersey (2.7)?. I imagine that i am missing some dependecy. The result from inferFile() is returning blank.

I can see that the method toStirng from Student class was not been called.

Maven

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.1.3.v20140225</version>
</dependency>

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlet</artifactId>
    <version>9.1.3.v20140225</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.14</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.14</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-http</artifactId>
    <version>2.14</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.14</version>
</dependency>

Java

public class Student {

    public Student(){
    }
    @Override
    public String toString(){
        return new StringBuffer(" First Name : ").append("MY FIRST NAME").toString();

    }
}

@Path("/bulkload")
public class BulkLoadAPI {
    @POST
    @Path("inference")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public Student inferFile() throws URISyntaxException, IOException {
        Student s = new Student();
        return s;
    }
}

public static void main(String[] args) throws Exception {
    ServletHolder jerseyServlet = new ServletHolder(ServletContainer.class);
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "service.api.BulkLoadAPI");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");

    Server server = new Server(10500);
    ServletContextHandler context = new ServletContextHandler (server, "/",       Servl   etContextHandler.SESSIONS);
    context.addServlet(jerseyServlet, "/*");
    server.start();
    server.join();
}

Upvotes: 2

Views: 1703

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

I'm not really sure what you're expecting. If you're expecting toString() to be called (which it isn't), that wouldn't even produce valid JSON. The way POJO to JSON (and vice versa) conversion is done is through MessageBodyReaders and MessageBodyWriters. Basically they are looking for fields with either some form of annotation known to the marshaller/unmarshaller, or Java bean style getters and setters. That's how the data/properties for the JSON will be discovered.

For example, if your Student looked like

public class Student {

    private String firstName;

    public String getFirstName() { 
        return firstName; 
    }

    public void setFirstName(String firstName) { 
        this.firstName = firstName; 
    }
}

it would give you JSON like {"firstName":"MyFirstName"}. You just need to set the property

public Student inferFile() throws URISyntaxException, IOException {
    Student s = new Student();
    s.setFirstName("MyFirstNaem");
    return s;
}

Another thing, this is not needed

setInitParameter("com.sun.jersey.api.json.POJOMappingFeature","true");

That is a Jersey 1 feature. You are using Jersey 2. See also

jersey.config.server.provider.packages

So you don't have to configure each class individually. The value should be a package. It will scan the package and sub packages for annotated classes.

Upvotes: 2

Related Questions