Dienstag, 18. September 2012

Jersey 1.9 with IntelliJ IDEA 11



Jersey 1.9 with IntelliJ IDEA 11

In this short tutorial we ll see how you can setup Jersey with IntelliJ IDEA 11. I have been new to IntelliJ IDEA so it took me a while to find it out. Here we go..

Development Environment
  • IntelliJ IDEA 11
  • TomCat 7
  • JDK7
Ressources

Steps

1. Create a new Maven Project in IntelliJ IDEA. Its not necessary to use an archetype of maven.

2. Right click on the project root and click "Add Framework Support", then check "Web" and continue. IntelliJ will add a "web" folder to your project.

3. Edit the pom.xml file an copy&paste the following dependencies for maven.


<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>CXMService</groupId>
    <artifactId>CXMService</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-grizzly2</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>
    
</project>

Note: In the Jersey documentation they recommend to use Version 1.9.18-i for Grizzly2; for now my installation wasnt able to resolve this dependency, so i took 1.9.1 and it worked.

4. Right click the pom.xml file and select Maven -> Reimport. Then the IDE will load all  libraries and shows them in "External libraries".

5. Setup "Tomcat" as your deployment plattform. To do so, click "Edit Configurations.." and setup TomCat. There is a nice PDF out in the wild, explaining how to setup your application server. See here.

6. Edit the web.xml within web/WEB-INF folder.

7. Copy&Paste the following code snippet to get Jersey running as serverlet. Alternate configurations can be found here.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
           version="3.0">
    <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>cxm.spider.test</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 REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

The web.xml will configure Jersey with JSON support. I marked in red two parameters you should change. Instead of "cxm.spider.test" give your package name, containing the root ressource classes (classes with @Path annotation). The url-pattern can be changed, its optional. Alternatives are i.e. /* or /myservice/*

8. Before you can deploy Jersey, open the Project Settings in IntelliJ IDEA (STRG-ALT-S) and navigate to "Artifacts".

9. Then select all available elements, right click and choose "Put in /WEB-INF/lib"

Now you should be able to start and deploy your service. But wait! The service is not yet finished. Its ready for deployment but it wont work before adding at least one ressource class (see Jersey documentation). If you miss out, the TomCat logs will show an  exception like "no ressource class found.".






Keine Kommentare:

Kommentar veröffentlichen