Setting up a Java REST Service in Eclipse with Maven

2 minute read

Information on how to migrate Java Web projects to maven.

Prepare

cd {EclipseWorkspace}
mvn archetype:generate -DgroupId=ch.htwchur.sii.rest -DartifactId=HelloRest -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false  
cd HelloRest  
mvn eclipse:eclipse -Dwtpversion=2.0

Import the project in Eclipse: File -> New… -> Java Project: Project Name: HelloRest

mvn eclipse:clean

HelloRest -> F5

HelloRest -> Configure -> Convert to Maven Project…

HelloRest -> Maven -> Update Project…

Configure Maven

pom.xml:

  • add dependency: jersey-bundle
  • add property: project.build.sourceEncoding UTF-8
  • specifiy jvm version:
<build>  
    ....  
    <plugins>  
        <plugin>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration>  
        </plugin>  
    </plugins>  
</build>  

HelloRest -> Maven -> Update Project…

HelloRest -> Properties -> Project Facets -> Java -> Change version: 1.7

Code

  • Create the folder src/main/java
  • Create a package in src/main/java, e.g.: ch.htwchur.sii.rest
  • Add a new Class: e.g.: HelloWorldService
package ch.htwchur.sii.rest;  
 
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.Response;  
   
@Path("/hello")  
public class HelloWorldService {  
 
   @GET  
   @Path("/{param}")  
   public Response getMsg(@PathParam("param") String msg) {  
       String output = "Jersey say : " + msg;  
       return Response.status(200).entity(output).build();`  
   }  
   
}

Register

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
   <display-name>`Restful Web Application`</display-name>  
   
   <servlet>  
       <servlet-name>jersey-serlvet</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>ch.htwchur.sii.rest</param-value>  
       </init-param>  
       <load-on-startup>1</load-on-startup>  
   </servlet>  
   
   <servlet-mapping>  
       <servlet-name>jersey-serlvet</servlet-name>  
       <url-pattern>/rest/*</url-pattern>  
   </servlet-mapping>  
</web-app>

Build

HelloRest -> Run: Maven Build: package

Check target/.../WEB-INF/... contains jersey-bundle jar

Run

Servers -> doubleclick Tomcat -> Tab: Modules -> Add external web module:

  • Document base: W:\EclipseWorkspace\HelloRest\target\HelloRest
  • Path: HelloRest

Fire up: http://localhost:8088/HelloRest/rest/hello/world

Run “Maven build: package” again after changes to the code, the web app will be reloaded automatically.

Resources

Converting a Java REST Service in Eclipse to use Maven and deploy to the package

Prerequisites

Convert

Sample Project: jeremia

Configure -> Convert to Maven Project:

  • Group Id:com.weblyzard
  • Artifact Id:jeremia
  • Packaging: war
  • Name: Jeremia

Configure Maven

Add the following entries to pom.xml to:

  • change type to web app
  • define jre/jdk v.7
<build>  
   <plugins>  
       <plugin>  
           <groupId>org.apache.maven.plugins</groupId>  
           <artifactId>maven-eclipse-plugin</artifactId>  
           <configuration>  
               <wtpapplicationxml>true</wtpapplicationxml>  
               <wtpversion>2.0</wtpversion>  
           </configuration>  
       </plugin>  
       <plugin>  
           <artifactId>maven-compiler-plugin</artifactId>  
           <configuration>  
               <source>1.7</source>  
               <target>1.7</target>  
           </configuration>  
       </plugin>  
   </plugins>  
</build>

run

mvn clean eclipse:eclipse

refresh the project

Reorganize the code

add folders

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources

Maven -> Update Project

  • Move source code files from src/com… to src/main/java
  • Move all Files containing org.junit to src/test/java
  • Move properties and config files to …/resources

Prepare for deployment

Add to pom.xml:

<repositories>  
<repository>  
       <id>code.semanticlab.net</id>  
       <url>[http://code.semanticlab.net/deploy/](http://code.semanticlab.net/deploy/)</url>  
   </repository>  
</repositories>

Add dependencies

  • junit
  • apache jcs (without the pom)
  • stanford-postagger-models (manually in xml)

Run tests

Try to run the tests both ways:

  • run test package as junit
  • run as maven build… with goal: package

the tests’ setups are different. So if something fails, see Maven#Troubleshooting

Deploy

Once you run maven build package, there might still be some warnings:

  • add missing versions by running: mvn help:effective-pom
  • add default log4j.properties
  • …?

Add the wagon to pom.xml

<build>  
   <extensions>  
       <extension>  
           <groupId>org.apache.maven.wagon</groupId>  
           <artifactId>wagon-webdav-jackrabbit</artifactId>  
           <version>1.0-beta-7</version>  
       </extension>  
   </extensions>  
   ...

run

mvn deploy:deploy-file -DpomFile=pom.xml -Dfile=target\jeremia-0.0.1-SNAPSHOT.war -DrepositoryId=code.semanticlab.net -Durl=dav:http://code.semanticlab.net/deploy/

Hint

To deploy on Windows to a passwordprotected url consider:

  • credentials set in [user folder]\.m2\settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  <servers>  
    <server>  
      <id>code.semanticlab.net</id>  
      <username>alibaba</username>  
      <password>sesam open up</password>  
    </server>  
  </servers>  
</settings>
  • java_home set to a up-to-date Java Version (i.e. JDK 1.7)

set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_25

  • then just run:

mvn deploy

Troubleshooting

sometimes when resources in packages i.e. com.weblyzard…splitter.data wont get loaded:

  1. maven clean
  2. project clean
  3. run tests as junit

Publish Libraries to Maven Repository

  • To publish a third party library to a repository, the following command can be used:

mvn 
deploy:deploy-file -DgroupId=` -DartifactId=`` -Dversion=`` -Dpackaging=`` -Dfile=`` ` `-DrepositoryId=`` -Durl=`` `

  • Example: Deploy the jar-file stanford-parser.jar to the semanticlab maven repository

mvn deploy:deploy-file -DgroupId=com.weblyzard.thirdparty -DartifactId=stanford-parser -Dversion=3.3.1 -Dpackaging=jar -Dfile=stanford-parser.jar 
-DrepositoryId=code.semanticlab.net -Durl=dav:http://code.semanticlab.net/deploy/

Resources

   <dependency>  
         <groupId>`com.sun.jersey`</groupId>  
         <artifactId>jersey-servlet</artifactId>  
         <version>1.13</version>  
    </dependency>`      `

Categories: ,

Updated:

Leave a comment