java - Setting up Jax-rs 2.2 with Jetty -
i'm trying set rest services on jetty using jersey jaxrs. can't json data through rest service class though. ajax requests keep getting "unsupported media type" error , status. regardless of @produces
, @consumes
annotations add methods though should both mediatype.application_json
.
i can't find decent documentation on jersey , loads of questions, blogs, , other resources seem out of date. looks jersey has undergone lot of changes , i'm @ loss should looking. set following based on jersey webapp archetype:
web.xml:
<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.my.package.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey web application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
code snippet:
@path("/users") public class userservice { // plain text works! @get @consumes(mediatype.text_plain) public string list(){ return "got it!"; } // json doesn't work! >:( @post @consumes(mediatype.application_json) @produces(mediatype.application_json) public user create(user user) { mocks.users.add(user); return user; }
my parent pom manages these dependencies ahd second of these 2 dependency in jax-rs project pom.
<dependency> <groupid>org.glassfish.jersey</groupid> <artifactid>jersey-bom</artifactid> <version>2.2</version> <type>pom</type> </dependency> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet-core</artifactid> <version>2.2</version> </dependency>
do need add support json?
have read chapter dedicated json in users guide? easiest way adding dependency on moxy , json support work out-of-the-box (you don't need explicitly register features modules provides make work opposed other json modules in jersey):
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-moxy</artifactid> <version>2.2</version> </dependency>
anyways jersey provides more modules handling json media type:
- moxy (examples: json-moxy, bean-validation-webapp)
- jackson (example: json-jackson)
- java api json processing (json-p) (example: json-processing-webapp)
- jettison (example: json-jettison)
Comments
Post a Comment