I've built a project with Spring JPA, and now I want to use it in my Jersey project. I've added my SJPA project as a dependency in my pom.xml
I would like to use my service classes from my SJPA when I use GET/POST/PUT/DELETE methods.
Is there an easy way to do this with annotations? Or do I have to get AnnotationConfigApplicationContext
in each class? Feels kind of waste.
@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();
}
}
NOTE: The linked example projects below are from the Jersey master branch, which is currently a snapshot of Jersey 3, which is not yet released. Jersey 3 will be using Spring 4, so you may notice a dependency
jersey-spring4
. This dependency does not exist yet, as Jersey 3 is not yet released (probably not for a while). So the dependency to use isjersey-spring3
. All the example should still work the same, just changing that one dependency. If you want to use Spring 4, see the dependencies listed in the example pom below in this answerYou don't need to create the
ApplicationContext
where you need the service. You should be able to configure a global one. Jersey has a module for this that integrates the two frameworks. This allows you to simply@Autowired
all your Spring services into your Jersey resource classes.Instead of trying to produce any example, I will just link to the official examples. They are straight from the projects, so the links should be good for some time. Take special not of the Maven dependencies. You will need to make sure to have them for the example to work.
Note: The
${spring3.version}
version in the examples is 3.2.3.RELEASE. It's possible to use Spring 4 with the examples, but you will need to make sure to exclude all the Spring transitive dependencies from thejersey-spring3
dependency.[1] - One thing to note about the Java config example is that it uses a standalone app. To use Java config in a webapp requires a bit of trickery. This is a known bug where Jersey looks for an param
contextConfigLocation
with the location of theapplicationContext.xml
file and will throw an exception when it doesn't find one.I've found a few ways around this.
An example of this was mentioned by the person who raised the issue. You can create a Spring web initializer where you configure the spring context and override the param property. (See full example here).
You could simply add the
applicationContext.xml
to the classpath and just register the spring Java configuration class as a beanThere's another way I can think of, but I've save that for a time I can actually test it out.
UPDATE
Seems to be related to this, using Spring 3 with Java 8. Like I said, if you want to use Spring 4, you will need to exclude the Spring transitive dependencies from
jersey-spring3
and just change the version of your explicitly declared Spring dependencies. Here is an example, that I tested and works.