• Now all data be ready for publish, We had made our blog alive now. Haha.

  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…

Combining Spring project and Jersey- Stack Overflow

Java PingBook 2 months ago (10-23) 14 0


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.

public final class UserResource
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private PodcastService service;

public Response getAllPodcasts() {
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 is jersey-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 answer

You 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.

Jersey 2.x with Spring XML config
Jersey 2.x with Spring Java config [1]
Jersey 1.x with Spring XML config
Jersey 2.x with Spring Boot

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 the jersey-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 the applicationContext.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).

public class SpringWebContainerInitializer implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {

// 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();
return context;
You could simply add the applicationContext.xml to the classpath and just register the spring Java configuration class as a bean

There’s another way I can think of, but I’ve save that for a time I can actually test it out.


“Failed to read candidate component class … ASM ClassReader failed to parse class file – probably due to a new Java class file version that isn’t supported yet”

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.










Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Combining Spring project and Jersey- Stack Overflow
LIKE (0)
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website