整合Jersey2&关于STS的Spring4

时间:2017-01-26 12:36:03

标签: maven-3 jersey-2.0 spring-tool-suite spring-4

我正在尝试在带有Spring MVC项目的STS包上的“Java Web服务,启动并运行”的第2章中实现“作为JAX-RS资源的静态Web服务”示例。 (我修改了颂歌,所以我猜没有版权侵犯)

这本书谈到使用“com.sun.jersey.spi.container.servlet.ServletContainer”(可能是Jersey 1.x?)。但是我想试试最新的Jersey2(glassfish)。我通过下面的链接和stackoverflow上的许多类似链接讨论了不同的maven依赖关系& web.xml配置。这些资源都不起作用。

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>

另外,如何使用URL访问资源?它是否与POM工件ID有任何依赖关系?

我总是低于http://localhost:8080/myapp/adages/

的错误
HTTP Status 404 - Not Found
type Status report
message Not Found
description The requested resource is not available.

0 个答案:

没有答案