mer*_*tje 7 java spring jax-rs jersey
我用Spring JPA构建了一个项目,现在我想在我的Jersey项目中使用它.我已将我的SJPA项目添加为我的pom.xml中的依赖项
当我使用GET/POST/PUT/DELETE方法时,我想使用我的SJPA中的服务类.使用注释有一种简单的方法吗?或者我必须AnnotationConfigApplicationContext参加每堂课?感觉有点浪费.
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class UserResource
{
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private PodcastService service;
@GET
public Response getAllPodcasts() {
context.scan("org.villy.spring.service");
context.refresh();
service= context.getBean(PodcastService.class);
return Response.ok(service.findAll()).build();
}
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*tha 13
注意:下面链接的示例项目来自Jersey master分支,该分支目前是Jersey 3的快照,尚未发布.Jersey 3将使用Spring 4,因此您可能会注意到依赖性jersey-spring4.这种依赖性尚不存在,因为泽西3还没有发布(可能暂时还没有).所以使用的依赖是jersey-spring3.所有示例仍应该相同,只需更改一个依赖项.如果您想使用Spring 4,请参阅本答案中下面示例pom中列出的依赖项
您无需创建ApplicationContext需要服务的位置.您应该能够配置全局的.Jersey有一个模块,它集成了两个框架.这允许您将@Autowired所有Spring服务简单地放入Jersey资源类中.
我只是链接到官方的例子,而不是试图产生任何例子.它们直接来自项目,因此链接应该是好的一段时间.特别注意Maven依赖项.您需要确保让它们用于示例才能工作.
注意:${spring3.version}示例中的版本是3.2.3.RELEASE.可以将Spring 4与示例一起使用,但是您需要确保从jersey-spring3依赖项中排除所有Spring传递依赖项.
[ 1 ] - 关于Java配置示例需要注意的一点是它使用独立的应用程序.要在Web应用程序中使用Java配置,需要一些技巧.这是一个已知的错误,其中Jersey查找contextConfigLocation带有applicationContext.xml文件位置的param ,并在找不到时抛出异常.
我已经找到了一些方法.
提出问题的人提到了这方面的一个例子.您可以创建一个Spring Web初始化程序,您可以在其中配置spring上下文并覆盖param属性.(参见此处的完整示例).
@Order(1)
public class SpringWebContainerInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerContextLoaderListener(servletContext);
// Set the Jersey used property to it won't load a ContextLoaderListener
servletContext.setInitParameter("contextConfigLocation", "");
}
private void registerContextLoaderListener(ServletContext servletContext) {
WebApplicationContext webContext;
webContext = createWebAplicationContext(SpringAnnotationConfig.class);
servletContext.addListener(new ContextLoaderListener(webContext));
}
public WebApplicationContext createWebAplicationContext(Class... configClasses) {
AnnotationConfigWebApplicationContext context;
context = new AnnotationConfigWebApplicationContext();
context.register(configClasses);
return context;
}
}
Run Code Online (Sandbox Code Playgroud)您可以简单地将其添加applicationContext.xml到类路径中,并将spring Java配置类注册为bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="config" class="com.your.pkg.SpringAnnotationConfig"/>
</beans>
Run Code Online (Sandbox Code Playgroud)还有另一种我能想到的方式,但我已经保存了一段时间,我可以实际测试它.
"无法读取候选组件类... ASM ClassReader无法解析类文件 - 可能是由于尚未支持的新Java类文件版本"
似乎与此相关,使用Spring 3和Java 8.就像我说的,如果你想使用Spring 4,你需要排除Spring传递依赖,jersey-spring3并且只需要改变你明确声明的Spring依赖的版本.这是一个例子,我测试和工作.
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7276 次 |
| 最近记录: |