作者:书友33947127 | 来源:互联网 | 2023-05-28 13:57
我正在尝试制作一个没有web.xml的Servlet 3.0 REST服务器,即仅依赖于java类和注释.我不确定如何同时使用Spring和servlet映射注册Resource类.
我目前有:
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的内容.
有谁知道如何以这种方式将Jersey与Spring集成?
1> Paul Vargas..:
您可以将Spring和Jersey集成在无XML模式中.
您将需要扩展该类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
).
以下代码是资源的示例:
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();
}
}
Spring需要扫描这些资源以进行依赖注入.例如:
package org.test.rest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.test.rest")
public class RestConfiguration {
}
此配置可由初始化程序类加载.例如:
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
4.0.0
org.test
jersey-spring
1.0-SNAPSHOT
war
jersey-spring
${project.build.directory}/endorsed
UTF-8
org.springframework
spring-context
4.1.4.RELEASE
org.springframework
spring-webmvc
4.1.4.RELEASE
org.springframework
spring-web
4.1.4.RELEASE
com.sun.jersey.contribs
jersey-spring
1.18.3
org.springframework
spring-context
org.springframework
spring-web
org.springframework
spring-core
org.springframework
spring-beans
org.springframework
spring-aop
javax
javaee-web-api
7.0
provided
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
${endorsed.dir}
org.apache.maven.plugins
maven-war-plugin
2.3
false
org.apache.maven.plugins
maven-dependency-plugin
2.6
validate
copy
${endorsed.dir}
true
javax
javaee-endorsed-api
7.0
jar