How To Enable Jersey Residual Api Inwards A Maven Spider Web Project

This tutorial assumes that you lot already know how to usage a maven projection inwards eclipse. Steps: 1.) Create a novel maven spider web project. (this assumes that you lot convey maven plugin installed inwards your eclipse). 2.) In your META-INF/web.xml file brand certain that you lot convey the ff code:
 <servlet>  <servlet-name>Jersey REST Service</servlet-name>  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  <init-param>   <param-name>com.sun.jersey.config.property.packages</param-name>   <param-value>com.ipiel.xxx</param-value>  </init-param>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>  <servlet-name>Jersey REST Service</servlet-name>  <url-pattern>/api/*</url-pattern> </servlet-mapping> 
3.) Note the value of com.sun.jersey.config.property.packages, this tells us that it volition seek out the classes within com.ipiel.xxx for REST enabled spider web services.
 @Path("/hello") populace cast Hello {  @GET  @Path("/world")  populace Response sayHello() {   String howdy = "Hello World!";   provide Response.status(200).entity(hello).build();  } } 
4.) Make certain that you lot convey the ff Definition inwards your pom.xml
 <dependency>  <groupId>com.sun.jersey.contribs</groupId>  <artifactId>jersey-spring</artifactId>  <version>${jersey.version}</version>  <exclusions>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>   </exclusion>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring</artifactId>   </exclusion>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>   </exclusion>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring-web</artifactId>   </exclusion>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring-beans</artifactId>   </exclusion>   <exclusion>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>   </exclusion>  </exclusions> </dependency> 
5.) Compile in addition to deploy the war/ear inwards a container (jboss, glassfish, etc). 6.) Now you lot tin access the spider web app inwards your localhost: http://localhost:port/appname/api/hello/world And it should output "Hello World!". Note: In illustration you lot desire to add together parameter to your spider web service, this is how you lot volition code it:
 @Path("/reverse") @POST @Consumes("application/x-www-form-urlencoded") @Produces({ MediaType.APPLICATION_JSON }) populace Response reverse(@FormParam("orderId") long orderId) { } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.