基于注释的Spring / JAX-RS集成,没有web.xml [英] Annotation based Spring / JAX-RS integration with no web.xml

查看:99
本文介绍了基于注释的Spring / JAX-RS集成,没有web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个没有web.xml的Servlet 3.0 REST服务器,即仅依赖于java类和注释。我不确定如何同时使用Spring和servlet映射注册Resource类。

I'm trying to make a Servlet 3.0 REST server with no web.xml, ie dependent on java classes and annotations only. I'm not exactly sure how I can simultaneously register a Resource class with Spring and with the servlet mappings.

我目前有:

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
    @Override
    protected Class<?>[] getRootConfigClasses()
    {
        return new Class<?>[]{RootConfiguration.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses()
    {
        return new Class<?>[]{RestServletConfiguration.class};
    }

    @Override
    protected String[] getServletMappings()
    {
        return new String[] {"/*"};
    }
}


@Configuration
@ComponentScan({"com.my.spring.beans", 
               "com.my.spring.services"})
public class RootConfiguration 
{
}

@Configuration
@ComponentScan("com.my.resource.classes")
public class RestServletConfiguration
{
}

当我开始应用程序,spring bean正确连接,但我不知道如何正确设置JAX-RS servlet过滤器。我目前正在使用Jersey,并且在网上没有找到很多关于将Jersey与Spring集成而不使用web.xml的信息。

When I start the application, the spring beans are wired correctly, but I'm not sure how to proceed with getting the JAX-RS servlet filter set up correctly. I am currently using Jersey, and haven't been able to find much on the web about integrating Jersey with Spring without using a web.xml.

有谁知道怎么做以这种方式将Jersey与Spring集成在一起?

Does anyone know how to integrate Jersey with Spring in this way?

推荐答案

您可以在无XML模式下集成Spring和Jersey。

You can integrate Spring and Jersey in a XML-less mode.

您需要扩展类 com.sun.jersey.spi.spring.container.servlet.SpringServlet 。例如:

package org.test;

import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = {"/rest/*"}, initParams = {
    @WebInitParam(name = "com.sun.jersey.config.property.packages",
            value = "org.test.rest")})
public class JerseyServlet extends SpringServlet {

}

实际上,它是一个servlet。参数 com.sun.jersey.config.property.packages 表示扫描资源的位置(带注释的类 @Path )。

Actually, it is a servlet. The parameter com.sun.jersey.config.property.packages indicates where scanning the resources (classes with annotation @Path).

以下代码是资源示例:

package org.test.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.test.service.DummyService;

@Path("test")
@Component
public class TestResource {

    @Autowired
    private DummyService service;

    @GET
    public String test() {
        return service.sayHello();
    }

}

此类资源需要扫描依赖注入的春天。例如:

Such resources need to be scanned by Spring for dependency injection. e.g.:

package org.test.rest;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("org.test.rest")
public class RestConfiguration {

}

此配置可由初始化程序类加载。例如:

And this configuration can be loaded by the initializer class. e.g.:

package org.test;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.test.rest.RestConfiguration;
import org.test.service.ServiceConfiguration;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ServiceConfiguration.class, RestConfiguration.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

}

也许你想看到 pom.xml

<?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>org.test</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>jersey-spring</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.18.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这篇关于基于注释的Spring / JAX-RS集成,没有web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆