0

I'm trying to get very simple POJO mapping to work in a fresh application. I'm following several online examples, but nothing works. I always get

Severe:   MessageBodyWriter not found for media type=application/json...

from GlassFish (4.0)

My app startup is right out of the Jersey quickstart guide:

@ApplicationPath("/")
public class App extends ResourceConfig {
    public App() {        
       final MoxyJsonConfig moxyJsonConfig = new MoxyJsonConfig();
       Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
       namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
       moxyJsonConfig.setNamespacePrefixMapper(namespacePrefixMapper).setNamespaceSeparator(':');
       register(moxyJsonConfig);
}

}

And the resource class is also right out of the guide:

  @Path("/")
  public class PojoTestService {

     @GET
     @Produces(MediaType.APPLICATION_JSON)
        return new Pojo();
     }
}

The POJO is well, just a POJO! I've tried it with and without @XMLRootElement, since I found contradictory information on if that should be included or not...

my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.mycom</groupId>
<artifactId>artifact</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>artifact</name>

<build>
    <finalName>artifact</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>
<properties>
    <jersey.version>2.13</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <!-- This web.xml file is not required when using Servlet 3.0 container,
  see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.mycom.server.package</param-value>
        </init-param>
        <init-param>
           <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
           <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

What have I forgotten to configure?

BadZen
  • 4,083
  • 2
  • 25
  • 48
  • There are no errors in the startup log, and other mappings work just fine. Application class is being found and called, resource class is being found and called, according to a debugger run... – BadZen Nov 04 '14 at 00:20
  • What does your POJO look like? – bdoughan Nov 04 '14 at 16:07

1 Answers1

0

Looks like you're using config-keys from Jersey 1.x, starting with "com.sun.jersey...". In Version 2.x of Jersey it has changed, as you can perhaps see in this key:

jersey.config.server.provider.packages 

I've got it running by creating an empty Application class in the top package of your webservice classes:

package path.to.your.ws;

import javax.ws.rs.core.Application;

public class WSApp extends Application {
}

In the web.xml you've just point to this application class and your other classes in this package and below will be registred.

<servlet>
    <servlet-name>REST-Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>path.to.your.ws.WSApp</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST-Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Instead MOXy you can also try following jersey media module (I have not compared them):

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>

Also I recomend reading this article

http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/

eeezyy
  • 2,109
  • 1
  • 15
  • 18