• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Samples | JAX-RS: Sample Application (Jersey, Spring, Tomcat)
  1. The application structure
  2. The POM file (pom.xml)
  3. web.xml
  4. applicationContext.xml
  5. MyResourceConfig.java
  6. REST service: MyEntityRestAPI.java
  7. Java entity: MyEntity.java
  8. Build and test the rest application

  1. The application structure

    mtitek-rest-sample-eclipse
  2. The POM file (pom.xml)
    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>mtitek.rest.sample</groupId>
        <artifactId>mtitek-rest-sample</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.1.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>2.29.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>2.29.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.inject</groupId>
                <artifactId>jersey-hk2</artifactId>
                <version>2.29.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
  3. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">
    
        <servlet>
            <servlet-name>MyServletContainer</servlet-name>
    
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    
            <init-param>
                <param-name>javax.ws.rs.Application</param-name>
                <param-value>mtitek.rest.sample.MyResourceConfig</param-value>
            </init-param>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>MyServletContainer</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>
  4. applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    </beans>
  5. MyResourceConfig.java
    package mtitek.rest.sample;
    
    import org.glassfish.jersey.server.ResourceConfig;
    
    public class MyResourceConfig extends ResourceConfig {
    }

    Note that for this case "MyResourceConfig" is useless as there's no specific code added to this class.
    You can run your code without it, just replace its full path in the file "web.xml" by "org.glassfish.jersey.server.ResourceConfig".
  6. REST service: MyEntityRestAPI.java
    package mtitek.rest.sample;
    
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    @Path("rest/myEntity")
    @Produces({ MediaType.APPLICATION_XML })
    public class MyEntityRestAPI {
        @GET
        @Path("/")
        public Response getMyEntity() {
            MyEntity myEntity = new MyEntity();
    
            myEntity.setId("myEntity - id - 1");
            myEntity.setCode("myEntity - code - 1");
            myEntity.setDesc("myEntity - desc - 1");
    
            return Response.ok(myEntity).build();
        }
    }
  7. Java entity: MyEntity.java
    package mtitek.rest.sample;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement(name = "myEntity")
    @XmlAccessorType(XmlAccessType.NONE)
    public class MyEntity {
        @XmlAttribute(name = "id")
        private String id;
    
        @XmlElement(name = "code")
        private String code;
    
        @XmlTransient
        private String desc;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(final String code) {
            this.code = code;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
  8. Build and test the rest application
    Apache Maven: mvn clean package

    Apache Tomcat: deploy the war file "mtitek-rest-sample-1.0.0-SNAPSHOT.war" in the folder "${TOMCAT_ROOT}/webapps/"

    REST client tool: http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity

    Response:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <myEntity id="myEntity - id - 1">
        <code>myEntity - code - 1</code>
    </myEntity>
© 2025  mtitek