hi
The example for "Jersey 2.7 and Spring integration" hosts here:
https://github.com/jersey/jersey/tree/2.7/examples/helloworld-spring-webapp.
I tested this example and found something maybe wrong.
My test steps are here:
1. modify the applicationContext.xml like this
<context:component-scan base-package="org.glassfish.jersey.examples.helloworld" />
<!--
<bean id="greetingService" class="org.glassfish.jersey.examples.helloworld.spring.GreetingServiceImpl"/>
<bean class="org.glassfish.jersey.examples.helloworld.spring.DateTimeService" scope="request"/>
<bean class="org.glassfish.jersey.examples.helloworld.spring.SpringSingletonResource"/>
<bean class="org.glassfish.jersey.examples.helloworld.spring.SpringRequestResource" scope="request"/>
<bean class="org.glassfish.jersey.examples.helloworld.spring.CustomExceptionMapper"/>
-->
2. modify the org.glassfish.jersey.examples.helloworld.spring.DateTimeService.java like this: (add @Service annotation)
@Service
public class DateTimeService {
/**
* Get current date and time.
*
* @return current date.
*/
public Date getDateTime() {
return new Date();
}
}
3. modify the org.glassfish.jersey.examples.helloworld.spring.GreetingServiceImpl.java like this: (add @Service annotation)
@Service
public class GreetingServiceImpl implements GreetingService {
@Override
public String greet(String who) {
return String.format("hello, %s!", who);
}
}
4. modify the org.glassfish.jersey.examples.helloworld.spring.JerseyResource.java like this: (add @Service annotation and replace the fields annotation with @Resource)
@Path("jersey-hello")
@Service
public class JerseyResource {
private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());
//_at_Autowired
@Resource
private GreetingService greetingService;
//_at_Autowired
//_at_Inject
@Resource
private DateTimeService timeService;
public JerseyResource() {
LOGGER.fine("HelloWorldResource()");
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
}
}
5. package the example and run it in Tomcat 7, and access
http://localhost:8080/jersey-hello in Chrome, get the NullPointerException,the GreetingService & DateTimeService not injected in JerseyResource;.
if I modify the JerseyResource.java like this, it works fine( replace @Service with @Component).
@Path("jersey-hello")
@Component
public class JerseyResource {
private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());
//_at_Autowired
@Resource
private GreetingService greetingService;
//_at_Autowired
//_at_Inject
@Resource
private DateTimeService timeService;
public JerseyResource() {
LOGGER.fine("HelloWorldResource()");
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
}
}
or if I modify the JerseyResource.java like this, it works fine too ( replace @Resource with @Autowired or @Inject).
@Path("jersey-hello")
@Service
public class JerseyResource {
private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());
@Autowired
private GreetingService greetingService;
//_at_Autowired
@Inject
private DateTimeService timeService;
public JerseyResource() {
LOGGER.fine("HelloWorldResource()");
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
}
}
So, It is strange that the @Service and @Resource annotations cannot used in JerseyResource.java in same time.