0

I am trying to implement the 'A Restful Web Service as JAX-RS resource' example from chapter-2 of "Java Web Services, Up and Running" on an STS bundle with a Spring MVC project. (I modified ode, so I guess no copyright violation)

The book talks about using "com.sun.jersey.spi.container.servlet.ServletContainer" (probably Jersey 1.x ?). However I wanted to try out the latest Jersey2 (glassfish). I went through this link below and many such similar links on stackoverflow which talk about different maven dependencies & web.xml configurations. None of these resources work.

Integrating Jersey 2 and Spring with Java Based Configuration

Web.xml

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param>

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class>
    <init-param>
         <param-name>jersey.config.server.provider.classnames</param-name>
         <param-value>com.test.myapp.jersey.AdageController</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

AdageController.java

@Component
@Path("/adages")
public class AdageController {

@GET
@Produces({MediaType.APPLICATION_XML})
public JAXBElement<Adage> getXml() {

    Adage adage = new Adage();
    adage.setMessage("Be Good, Do Good!");

    return toXml(adage);
}

@XmlElementDecl(namespace = "http://localhost:8080", name = "adage")
private JAXBElement<Adage> toXml(Adage adage) {
    JAXBElement<Adage> jaxbElement = new JAXBElement<Adage>(new QName("adage"), Adage.class, adage);
    return jaxbElement;
}

@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("/json")
public String getJson() {

    Adage adage = new Adage();
    adage.setMessage("Be Good, Do Good!");

    String json = null;
    try {
        json = new ObjectMapper().writeValueAsString(adage);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return json;
}

@GET
@Produces({MediaType.TEXT_PLAIN})
@Path("/plain")
public String getPlain() {

    return "Be Good, Do Good!";
}

}

Adage.Java

@XmlRootElement(name="adage")
public class Adage {

private String message;

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

pom.xml

<groupId>com.test</groupId>
<artifactId>myapp-jws</artifactId>
<name>MyApp</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>4.1.0.RELEASE</org.springframework-version>
    <org.aspectj-version>1.8.0</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>


    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Java Based Annotation Support -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.4</version>
    </dependency>

    <!-- Web Services -->

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.6</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>2.11</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-web</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>org.glassfish.jersey.core</groupId>
            </exclusion>
            <exclusion>
                <artifactId>
                    jersey-container-servlet-core
                </artifactId>
                <groupId>org.glassfish.jersey.containers</groupId>
            </exclusion>
            <exclusion>
                <artifactId>hk2</artifactId>
                <groupId>org.glassfish.hk2</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--  JSON -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>        
</dependencies>

Also, how do I access the resource using URL ? Does it have any dependency to the POM artifact id ?

I am always getting below error with http://localhost:8080/myapp/adages/

HTTP Status 404 - Not Found
type Status report
message Not Found
description The requested resource is not available.
Community
  • 1
  • 1
user691197
  • 927
  • 6
  • 20
  • 38
  • Why are you excluding the Jersey dependencies from `jersey-spring3`? – Paul Samsotha Jan 26 '17 at 13:06
  • You should check [this](http://stackoverflow.com/a/32357991/2587435) out. – Paul Samsotha Jan 26 '17 at 13:09
  • I made a lot of trials with just jersey servlets alone, excluding spring, it didnt work properly on STS. I switched to eclipse and executed the same project and the web service got executed just fine. I guess there is some kind of bug in STS bundle.. I will try jersey-spring dependency also in Eclipse IDE and post an answer.. – user691197 Feb 02 '17 at 18:19

0 Answers0