glassfish - Why is POJO JSON mapping feature (MOXy) in Jersey not working in this simple example? -
glassfish - Why is POJO JSON mapping feature (MOXy) in Jersey not working in this simple example? -
i'm trying simple pojo mapping work in fresh application. i'm next several online examples, nil works.
severe: messagebodywriter not found media type=application/json...
from glassfish (4.0)
my app startup right out of bailiwick of 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 resource class right out of guide:
@path("/") public class pojotestservice { @get @produces(mediatype.application_json) homecoming new pojo(); } }
the pojo well, pojo! i've tried , without @xmlrootelement, since found contradictory info on if should 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"?> <!-- web.xml file 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 forgotten configure?
looks you're using config-keys bailiwick of jersey 1.x, starting "com.sun.jersey...". in version 2.x of bailiwick of jersey has changed, can perhaps see in key:
jersey.config.server.provider.packages
i've got running creating empty application class in top bundle of webservice classes:
package path.to.your.ws; import javax.ws.rs.core.application; public class wsapp extends application { }
in web.xml you've point application class , other classes in bundle , below 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 can seek next bailiwick of 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 recomend reading article
http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/
glassfish jackson pojo moxy
Comments
Post a Comment