/java/

Jersey

2013-06-25 19:25:27

http://www.vogella.com/articles/REST/

package pl.btbw.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloApi {

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\&quot;1.0\&quot;?>" + "

<hello> Hello Jersey" + "</hello>

";
  }

  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "

<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</h1></body></html>

" + " ";
  }

} 
package pl.btbw.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import pl.btbw.entity.Post;

@Path("/post")
public class PostApi {

  @GET
  @Path("/1")
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public Post getPostJson() {
    return new Post(1,"lorem","ipsum");
  }

} 
package pl.btbw.entity;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Post {
    private int id;
    private String title;
    private String desc;

    public Post(){

    }

    public Post(int id, String title, String desc){
        this.id = id;
        this.title = title;
        this.desc = desc;
    }

    //set
    //get
}

/WEB-INF/web.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<web-app http:="" id='"WebApp_ID"' java.sun.com="" javaee="" ns="" version='"2.5"' web-app_2_5.xsd&quot;="" xml="" xmlns='"http://java.sun.com/xml/ns/javaee"' xmlns:web='"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://java.sun.com/xml/ns/javaee'>
<servlet>
<servlet-name>REST</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>pl.btbw.api</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>REST</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
            30
        </session-timeout>
</session-config>
</web-app>


<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>com.mycompany</groupid>
<artifactid>Jersey</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Jersey</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
</properties>
<dependencies>
<dependency>
<groupid>javax.ws.rs</groupid>
<artifactid>jsr311-api</artifactid>
<version>1.1.1</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-moxy</artifactid>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey.glassfish.v3.osgi</groupid>
<artifactid>jersey-gf-server</artifactid>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-client</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-json</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-multipart</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-core</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>org.jvnet.mimepull</groupid>
<artifactid>mimepull</artifactid>
<version>1.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey.glassfish.v3.osgi</groupid>
<artifactid>jersey-gf-statsproviders</artifactid>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-grizzly</artifactid>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>org.codehaus.jettison</groupid>
<artifactid>jettison</artifactid>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey.glassfish.v3.osgi</groupid>
<artifactid>jersey-gf-servlet</artifactid>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>org.glassfish.hk2.external</groupid>
<artifactid>asm-all-repackaged</artifactid>
<version>1.1.15</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-atom</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-server</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-atom-abdera</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-apache-client</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-spring</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-guice</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs</groupid>
<artifactid>jersey-simple-server</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs.jersey-oauth</groupid>
<artifactid>oauth-client</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs.jersey-oauth</groupid>
<artifactid>oauth-server</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>com.sun.jersey.contribs.jersey-oauth</groupid>
<artifactid>oauth-signature</artifactid>
<version>1.13</version>
</dependency>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-web-api</artifactid>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version>2.3.2</version>
<configuration>
<source/>1.6
                    <target>1.6</target>
<compilerarguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerarguments>
</configuration>
</plugin>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-war-plugin</artifactid>
<version>2.1.1</version>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-dependency-plugin</artifactid>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputdirectory>${endorsed.dir}</outputdirectory>
<silent>true</silent>
<artifactitems>
<artifactitem>
<groupid>javax</groupid>
<artifactid>javaee-endorsed-api</artifactid>
<version>6.0</version>
<type>jar</type>
</artifactitem>
</artifactitems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>