1

I'm using a jetty server with jeresy. I'm trying to post a json with a integer/float values, when the expected type (Pojo in the code) is Object (/Serializable). Jersey converts the number to type com.sun.org.apache.xerces.internal.dom.ElementNSImpl (Or to a String representing the number in case I'm using Serializable).

Am I missing something?

Resource:

@Path("/entry-point")
public class EntryPoint {

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public Pojo post(Pojo pojo) {
        return pojo;
    }
}

The Object:

public class Pojo implements Serializable{
    public int i;
    public Object object;
}

The Jetty Server:

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);


    ServletHolder jerseyServlet = context.addServlet(
            org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    // Tells the Jersey Servlet which REST service/class to load.
    jerseyServlet.setInitParameter(
            "jersey.config.server.provider.classnames",
            EntryPoint.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } finally {
        jettyServer.destroy();
    }

And the pom.Xml:

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.2.3.v20140905</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.3.v20140905</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.7</version>
    </dependency>
gilad a
  • 39
  • 3
  • a Plain Old Java Object (POJO) is not allowed to implement or extend another class. – Lucas Jun 07 '16 at 12:32
  • You're probably better off using Jackson (rather than MOXy). MOXy is probably just calling the `toString` as it doesn't know the type. Aside from just this issue, I'd recommend Jackson all-around over MOXy. Switch to `jersey-media-json-jackson` and register the `JacksonFeature` – Paul Samsotha Jun 07 '16 at 13:05
  • @peeskillet I'm not sure if I'm doing so correctly (or more precisely, I'm sure I dont), here are my changes: ServletHolder servletHolder = new ServletHolder(new ServletContainer(new ResourceConfig(JacksonFeature.class))); context.addServlet( servletHolder, "/*"); servletHolder.setInitOrder(0); Now I my POST doesnt even get to my entry point – gilad a Jun 08 '16 at 07:00
  • You still need to register your endpoint with the ResourceConfig. Have a look at [this post](http://stackoverflow.com/a/28734049/2587435) – Paul Samsotha Jun 08 '16 at 11:00

1 Answers1

0

You should be using java.lang.Number in your Pojo instead of Object to store numeric values.

Vijay
  • 542
  • 4
  • 15
  • But I'm expecting also Strings. The backend logic will do the type handling – gilad a Jun 07 '16 at 12:41
  • Then you should map to String in your Pojo and handle the type/conversion in your code. – Vijay Jun 07 '16 at 12:44
  • I am a framework, I don't know the type I am receiving. I get some type, and pass it forward to the user. So if I get a String - I forward a String, if I get a double - I forward the double I have no flexibility on type management. – gilad a Jun 08 '16 at 06:49