Upgrading to switch from JSON/XML marshalling in order to use Jackson 2 - spring

I am currently using Spring - and am using Jersey's old technologies to marshall and unmarshall - I need to upgrade the marshalling library to the latest version in order to use Jackson.
I have usually got some code to show, but obviously in this scenario - I am not even sure where to begin, so if anyone can help out that would be cool.
WEB.XML CONFIGURATION FOR JERSEY :
<servlet>
<description>Jersey Servlet</description>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
POM :
<jersey.version>1.4</jersey.version>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${jersey.version}</version>
<scope>runtime</scope>
<!-- exclude spring dependencies so can pin to our desired versions (esp. spring-web) -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>

If you want to use Jackson 2, you should first get rid of the jersey-json, then add the following
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson2.version}</version>
<!-- latest right now is 2.8.3 -->
</dependency>
Then in your servlet configuration, add the Jackson package to the list of packages to scan.
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
your.packages,
com.fasterxml.jackson.jaxrs.json <!-- add jackson -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note that if you are using a Jackson version newer than 2.7.0, the above package will not include the Jackson ExceptionMappers. According to this issue, it seems they are now removed and also the #Provider annotations have been removed. So we can't package scan for them.
So if you do want the mappers, you should declare them by class.
<param-name>
com.sun.jersey.config.property.classnames
</param-name>
<param-value>
com.fasterxml.jackson.jaxrs.base.JsonMappingExceptionMapper,
com.fasterxml.jackson.jaxrs.base.JsonParseExceptionMapper
</param-value>
I should mention though, that it's really recommended to write your own mappers. The response the these mappers spit out, is the exception message, and that's not really something you want as the error response.

You will need to update your POM to include the jackson jars(core, databind, annotaion) in https://mvnrepository.com/artifact/com.fasterxml.jackson.core. You will also need to do the below for converting JSON to Java object and vice versa
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
You would need to create a class with variables that are named after the "keys" in JSON. The code to convert JSON string to object of the JAVA class would be as below:
Suppose JSON is
{
'name' : 'Test',
'userId' : 'test101'
}
The Java class corresponding to this would be:
public class Test {
private String name;
private String userId;
// Write getter and setter methods for each variable.
}
The code for converting JSON string to the object of Test class would be:
ObjectMapper mapper = new ObjectMapper();
String jsonInString = ""{'name' : 'Test', 'userId' : 'test101'}"";
//JSON from String to Object
Test obj = mapper.readValue(jsonInString, Test.class);
obj.getName() would give value corresponding to the key.

Related

Multiple Contexts For Request Scope?

I am attempting to use Weld with Tomcat7 and Jersey, and in my log files I am seeing this:
org.jboss.weld.exceptions.IllegalStateException: WELD-001304: More than one context active for scope type javax.enterprise.context.RequestScoped
The application deploys properly and I only see this when attempting to hit on my Jersey endpoints.
Here is the pertinent parts of the pom.xml
<!-- Jersey Deps -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<!-- CDI Deps -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.3.2.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
</dependency>
And here's my "MainApplication" that extends ResourceConfig:
public class MainApplication extends ResourceConfig {
public MainApplication() {
packages(true, "com.example.api");
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
}
}
Here's my web.xml (the servlet section):
<servlet>
<servlet-name>Jersey Servlet Container</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>net.di2e.isfr.foldr.MainApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Servlet Container</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
I am uber-confsued what could be going wrong.

Why is my session still alive

I am using spring with redis backed sessions.
If I access the site, I see that sessions is added to database.
Then I run flushdb on the database.
Then I try and access the site, and sessions is still alive (I have the same session id.) The redis keys are different, but the session id's are the same.
Is the session being stored somewhere else not in redis? Is this expected?
Using windows 64 bit redis 2.8.2400.
some pom dependencies.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
Relavent Beans
<beans:bean id="RedisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />
<beans:bean
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:port="6379" />
some web.xml
<listener>
<listener-class>com.mysite.listeners.sessionStarted</listener-class>
</listener>
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Controller function for closing session
RequestMapping(value = "/closeSession", method = RequestMethod.GET)
public String closeSession(HttpSession session, HttpServletRequest request) {
logger.info("Closeing Session (greri002 for testing).");
session.invalidate();
//request.logout();
SecurityContextHolder.getContext().setAuthentication(null);
return "OK";
}
Spring security is managing your sessions storing the data in redis. You only need to bring in something like redis if you're planning on being able to share session data between nodes.
Spring's SecurityContext where security info in stored.
https://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/core/context/SecurityContext.html
Assuming you're using a server that supports 3.0 Servlets you just need to call logout() on a HttpServletRequest object. A spring MVC sample -
#RequestMapping(value="/me/logout", method=RequestMethod.GET)
public #ResponseBody String logout(HttpServletRequest request) throws ServletException {
request.logout();
return "OK";
}
http://docs.spring.io/spring-security/site/docs/3.2.1.CI-SNAPSHOT/reference/htmlsingle/#servletapi-logout
If you don't have access to HttpServletRequest you can do
SecurityContextHolder.getContext().setAuthentication(null);
Also make sure you have logout for the http config config'ed correctly. he's mine
<sec:logout logout-url="/logout"
delete-cookies="JSESSIONID,jsessionid"
invalidate-session="true"
success-handler-ref="myLogoutFilter"/>

Access to the specified resource has been forbidden in simple Spring webservice Demo

I have created simple spring security demo with REST webservice.
I have spent lot of hours on It.Need strong pointers regarding simple working spring security for rest webservice with latest versions.
My Controller is
#Controller
public class RestContoller {
#RequestMapping(value = "/countryJSONProduce", method = RequestMethod.GET)
public ResponseEntity<CountryDetail> getCountryJSON() {
CountryDetail countryDetail = new CountryDetail("Values");
ResponseEntity<CountryDetail> rentity = new ResponseEntity<CountryDetail>(countryDetail, HttpStatus.OK);
return rentity;
}
#RequestMapping(value = "/countryJSONConsume", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public String consumeJSON(#RequestBody CountryDetail countryDetail) {
System.out.println("Country Detail Example");
return "home";
}
}
web.xml is
<servlet>
<servlet-name>springrest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springrest</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml
/WEB-INF/springrest-servlet.xml
</param-value>
</context-param>
my pom.xml is
<properties>
<spring.version>4.1.0.RELEASE</spring.version>
<springsecurity.version>4.0.2.RELEASE</springsecurity.version>
</properties>
<dependencies>
<!-- for Jsp use -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring mvc and Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring mvc and Core -->
<!-- JSON Response Spring Framework 4.1, the minimum jackson version should
be 2.1 -->
<!-- Compatiable Spring Framework 4.1 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<!-- Spring Authentication Start -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<!-- Spring Authentication End -->
</dependencies>
When I Run the code sometimes it shows popup in my Eclipse Mars INTENAL
browser for user and password.when i put my credentials it will goes to
localhost:8080/SpringMavenRest2/ welcome page ok.when I hit the
url localhost:8080/SpringMavenRest2/hello/countryJSONProduce
which is calling my first service.. it is showing the Error :Access to
the specified resource has been forbidden.403
Even I put user name and
password as basic auth.Now I am testing this second url FROM CHROME
POSTMAN CLIENT.
I am using this configuration Java 1.8 ,Tomcat 8.0
spring.version4.1.0.RELEASE ,springsecurity.version 4.0.2.RELEASE.
and maven 3.3
Its
working well without authentication.Could you give any best referenced demo
for spring security with basic authentication. I have refered this also
http://www.mkyong.com/spring-security/spring-security-hello-world-example/
Try this :
<security:intercept-url pattern="/hello/**" access="hasRole('ROLE_USER'') "/>
and / or
<security:intercept-url pattern="/**" access="hasAnyRole('IS_AUTHENTICATED_ANONYMOUSLY','ROLE_USER')"/>

Spring: context-component-scan is not working

There are lots of question have been asked around this question, but I am still not getting the root cause of what is preventing my bean to be autowired.
I have following level of packages:
com.pack.amg.service
com.pack.amg.repository
com.pack.amg.beans
com.pack.amg.resources
applicationContext.xml (which is required by ContextLoaderListner in web.xml)
<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"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.pack.amg" />
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg value="http://localhost:7474/db/data/" index="0" />
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService"
base-package="com.pack.amg.repository" />
<neo4j:repositories base-package="com.pack.amg.repository" />
<tx:annotation-driven mode="proxy" />
I am expecting here to auto scan all of my packages and subpackages and its classes using <context:component-scan base-package="com.pack.amg" /> which may be true but is not working at all..! Following is my REST resource:
TestResource.java
#Path("/")
#Service
public class TestResource
{
#Autowired
private MyService myService;
#Path("/save")
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response getResponse(ParamBeans bean)
{
System.out.println(bean);
String str = myService.save(bean); // <-- here myService is evaluating as null !!
return Response.ok(str).build();
}
}
MyService.java
#Service
public class MyService
{
#Autowired
private MyRepository repository;
public String save(ParamBeans bean)
{
return "Saved";
}
}
My war is deploying properly without any Exception, but while calling my REST service, it evaluating MyService as null. does anyone know why ? Thanks
M.Deinum is correct. You must need to make your Spring aware of your jersey. I had the same issue before, You need to add additional dependency and configuration in pom.xml as below:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.18.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
and replace your old implementation of Jersey servlet from web.xml with following one:
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.package.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern> <!-- depending upon your choice -->
</servlet-mapping>
Where com.sun.jersey.spi.spring.container.servlet.SpringServlet belongs to latest dependency added i.e. jersey-spring. Hope this helps
Update
As suggested by cronemberger, there seems some issues with Jersey 2 lib. So it may happen above implementation may not work with Jersey 2, but I need to check with the same.

jersey-spring3 instantiating Spring-managed bean (null!)

I first want to point out that this is by and large the biggest problem in terms of time wasted that I have ever dealt with in my career. (Over two days straight now with essentially 0 progress.) Every single "work-around" or "solution" I have attempted hasn't worked, so I am blocked and pretty desperate for some assistance.
The problem in a nutshell is that Jersey/HK2 seems to always instantiate my Spring-managed beans AFTER they have already been instantiated by Spring, which tells me that jersey-spring3 is not doing its job, or at least not with my current setup (or any of the ~50 permutations of setups I have tried thus far.)
Note that when I use an empty constructor, those resource fields are null at run-time.
I do not understand why my current setup doesn't work as I am essentially copying this online example
Any help is more than appreciated!!
Configuration
- - - - - pom.xml - - - - -
<!-- ... -->
<dependencies>
<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-parent</artifactId>
<version>${spring.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- / Spring Dependencies -->
<!-- API dependencies -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- / API dependencies -->
<!-- ... -->
</dependencies>
<!-- ... -->
<properties>
<!-- ... -->
<spring.version>3.0.5.RELEASE</spring.version>
<jersey.version>2.4.1</jersey.version>
<gson.version>2.2.4</gson.version>
<!-- ... -->
</properties>
<!-- ... -->
- - - - - web.xml - - - - -
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/beans.xml</param-value>
</context-param>
<!-- ... -->
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>fubar.rest.FubarJerseyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<!-- ... -->
</web-app>
- - - - - beans.xml (Context Configuration) - - - - -
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- ... -->
<!-- beans-outbound-api has configuration for spring-jersey3 to work properly -->
<import resource="beans-api.xml" />
</beans>
- - - - - beans-api.xml - - - - -
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Services -->
<bean id="locationServiceV1" class="fubar.rest.v1.services.location.impl.LocationServiceV1" />
<bean id="locationServiceV2" class="fubar.rest.v2.services.location.impl.LocationServiceV2" />
<bean id="viewServiceV1" class="fubar.rest.v1.services.view.impl.ViewServiceV1" />
<bean id="viewServiceV2" class="fubar.rest.v2.services.view.impl.ViewServiceV2" />
<!-- Resources -->
<bean class="fubar.rest.resources.location.impl.LocationResource">
<constructor-arg index="0" ref="locationServiceV1" />
<constructor-arg index="1" ref="locationServiceV2" />
</bean>
<bean class="fubar.rest.resources.view.impl.ViewResource">
<constructor-arg index="0" ref="viewServiceV1" />
<constructor-arg index="1" ref="viewServiceV2" />
</bean>
</beans>
Code
- - - - - Resource (JAX-RS) - - - - -
#Path(RESTLocationResourceV1.PathFields.PATH_ROOT)
#Produces({V1_JSON, APPLICATION_JSON})
public class LocationResource
extends ResourceBase<LocationResource, ILocationServiceV1, ILocationServiceV2> {
private static final Logger logger = Logger.getLogger(LocationResource.class);
#Inject
public LocationResource(final LocationServiceV1 v1Loc, final LocationServiceV2 v2Loc) {
super(v1Loc, v2Loc);
logger.info(format(Messages.INF_INSTANTIATED, "LocationResource"));
}
#GET
#Path(PathFields.SUBPATH_LIST)
public LocationListV1 listV1(#HeaderParam(HEADER_API_KEY) String apiKey)
throws ApplicationException {
// Implementation
}
#GET
#Path(PathFields.SUBPATH_SEARCH)
public LocationListV1 searchV1(#HeaderParam(HEADER_API_KEY) String apiKey,
#QueryParam(QueryFields.QUERY) String likeText) throws ApplicationException {
// Implementation
}
}
- - - - - Service (Spring Bean) - - - - -
public class LocationServiceV1 extends ServiceBaseV1<LocationBean, LocationV1, LocationListV1>
implements
ILocationServiceV1 {
#Autowired
private LocationDao daoLoc;
public LocationServiceV1() {
super(new LocationBeanToJsonTranslatorV1());
}
#Override
public LocationListV1 listV1() throws ApplicationException {
// Implementation
}
#Override
public LocationListV1 searchV1(String likeText) throws ApplicationException {
// Implementation
}
}
(Essentially the same for version 2)
- - - - - Application (Jersey) - - - - -
public class FubarJerseyApplication extends ResourceConfig {
private static final class Messages {
static final String INF_STARTING_APPLICATION = "Starting %s!";
}
private static final Logger logger = Logger.getLogger(FubarJerseyApplication.class);
public FubarJerseyApplication() {
packages("fubar.rest");
logger.info(format(Messages.INF_STARTING_APPLICATION, this.getClass().getName()));
}
}
Invocation (Client)
curl http://my-ip-address/fubar/api/location/list
(500 Internal Server Error)
Error (Server)
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object
available for injection at Injectee(requiredType=LocationServiceV1,parent=
LocationResource,qualifiers={}),position=0,optional=false,self=false,
unqualified=null,344016971)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:208)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:225)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:329)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:158)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2350)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:612)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:597)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:361)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at fubar.server.springframework.SessionFilter.doFilter(SessionFilter.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor$LoaderState.filter(ContextLoaderHttpInterceptor.java:75)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor$StartedState.filter(ContextLoaderHttpInterceptor.java:120)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor.doFilter(ContextLoaderHttpInterceptor.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:311)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Thread.java:662)
API Log
Dec 10, 2013 13:36:28 INFO [main] fubar.rest.FubarJerseyApplication
- Starting fubar.rest.FubarJerseyApplication!
Dec 10, 2013 13:38:06 INFO [pool-1-thread-1] resources.location.impl.LocationResource
- LocationResource has been instantiated
Dec 10, 2013 13:38:06 INFO [pool-1-thread-1] resources.view.impl.ViewResource
- ViewResource has been instantiated
Update -- found this:
Catalina Log
Dec 10, 2013 1:36:42 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4.1 2013-11-08 12:08:47...
Dec 10, 2013 1:36:43 PM org.glassfish.jersey.server.spring.SpringComponentProvider initialize
SEVERE: Spring context lookup failed, skipping spring component provider initialization.
Dec 10, 2013 1:38:00 PM com.sun.xml.bind.v2.runtime.reflect.opt.Injector inject
... so the ApplicationContext isn't found in SpringComponentProvider#initialize.
What's loading first? Spring or Jersey? It could be that your Spring context isn't initialized when SpringComponentProvider calls WebApplicationContextUtils.getWebApplicationContext(sc);. Try using Spring's ContextLoaderListener so that Spring does its initialization right after the app is deployed.
I ran into a lot of the same issues that you're experiencing with the jersey-spring3 library. It had problems finding my Spring ApplicationContext (looks like this is where you're stuck) and it blew up injecting setters that took a generic parameter as an argument.
If you get past the app context issue, I don't think what you have will work anyway. You defined the ViewResource and LocationResource beans in XML. From what I can tell, Jersey will only get the resource instance from Spring if the the resource class is annotated with #Component. Take a look at org.glassfish.jersey.server.spring.SpringComponentProvider, specifically component.isAnnotationPresent(Component.class):
// detect JAX-RS classes that are also Spring #Components.
// register these with HK2 ServiceLocator to manage their lifecycle using Spring.
#Override
public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
if (ctx == null) {
return false;
}
if(component.isAnnotationPresent(Component.class)) {
DynamicConfiguration c = Injections.getConfiguration(locator);
String[] beanNames = ctx.getBeanNamesForType(component);
if(beanNames == null || beanNames.length != 1) {
LOGGER.severe(LocalizationMessages.NONE_OR_MULTIPLE_BEANS_AVAILABLE(component));
return false;
}
String beanName = beanNames[0];
ServiceBindingBuilder bb = Injections.newFactoryBinder(new SpringComponentProvider.SpringManagedBeanFactory(ctx, locator, beanName));
bb.to(component);
Injections.addBinding(bb, c);
c.commit();
LOGGER.config(LocalizationMessages.BEAN_REGISTERED(beanName));
return true;
}
return false;
}
An unrelated issue was that we also wanted to move all of our JAX-RS annotations to interfaces. Whenever I tried it, I got "Could not find a suitable constructor for com.foo.ResourceInterface".
In the end, I solved all of my issues by not using jersey-spring3 and rolling my own Jersey to Spring connector. Here's what I did:
Configured all of my resources as regular Spring beans. You can use XML if you want.
In my Application, I added bindings to the HK2 container to use a factory whenever it needs an instance of one of the resources. My factory class simply returns the Spring managed instance of the resource.
Before the factory returns the Spring-managed bean, I use the Jersey/HK2 ServiceLocator to inject things that Jersey provides. For example, anything annotated with #Context.
My javax.ws.rs.Application looks like this:
public class RestConfig extends ResourceConfig {
private static final Log log = LogFactory.getLog(RestConfig.class);
#Inject
public RestConfig(ServiceLocator locator) {
super();
// specific to my app. get your spring beans however you like
Collection<Object> beans = BeanLocator.beansByAnnotation(RestResource.class);
DynamicConfiguration c = Injections.getConfiguration(locator);
for (Object bean : beans)
{
// tell jersey to use a factory for any interface that the bean implements. since your resources don't implement interfaces,
// you'll want to do something a bit different here.
for (Class<?> currentInterface : bean.getClass().getInterfaces())
{
if (log.isTraceEnabled())
log.trace("binding " + currentInterface.getSimpleName() + " to Spring managed bean");
ServiceBindingBuilder<Object> bb = Injections.newFactoryBinder(new StaticLookupFactory(locator, bean));
bb.to(currentInterface);
Injections.addBinding(bb, c);
}
}
// commit the changes to the HK2 container (don't skip this step!)
c.commit();
property("jersey.config.disableMoxyJson.server", true);
packages("com.foo.web.rest");
register(MoxyXmlFeature.class);
}
// a "factory" where the provide() method returns the spring managed bean
// that was passed to the constructor.
private static class StaticLookupFactory implements Factory<Object> {
private ServiceLocator locator;
private Object bean;
StaticLookupFactory(ServiceLocator locator, Object bean)
{
this.locator = locator;
this.bean = bean;
}
#Override
public Object provide() {
// inject this annotated with #Context, #Inject, etc
locator.inject(bean);
return bean;
}
#Override
public void dispose(Object instance) {
}
}
}
BeanLocator is a utility class that I wrote that makes it easy to grab bean instances using static methods when autowiring isn't available. For example, when working outside of Spring managed beans. Not too much going on there:
public static Collection<Object> beansByAnnotation(Class<? extends Annotation> annotation)
{
return applicationContext.getBeansWithAnnotation(annotation).values();
}
RestResource is also specific to our app. It's a custom stereotype that works like #Component, #Service, etc:
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Component
public #interface RestResource {
String value() default "";
}
Note that Jersey allows you to register custom implementations of org.glassfish.jersey.server.spring.ComponentProvider to manage the lifecycle of resources on your own. I tried it but couldn't get it to recognize my implementation no matter what I did.
One other note... the locator.inject(bean) call that activates the Jersey dependency injection mechanism will processes anything marked with #Inject. Use #Autowired within your classes or configure your beans with XML to avoid having both Spring and Jersey attempt to resolve values for things annotated with #Inject.
We have a custom, asynchronous ContextLoader, so the interim solution required placing a total hack in the Jersey-Spring3 source to wait for the application to initialize before the custom component provider initializes.
P.S. For any poor soul who finds themselves having to do something like this, make sure META-INF/settings contains the SpringComponentProvider configuration.
(2014-04-18) Elaborating for #Scott
Note that this is a terrible hack and I would only attempt such a thing as a last resort when all other attempts have failed, like in my case. Also I would consult the Jersey mailing group about your problem before attempting anything like this.
That said... this is what I did to solve my problem:
Literally copied the source code of spring-jersey3 into my application/server, modifying the header of every file with the appropriate tags as per the license;
Created the following class --
===>
/**
* Hack class for RN-8979.
*
* #author ryan
*
*/
public class ContextLoadWaiter {
private static final Logger logger = Logger.getLogger(ContextLoadWaiter.class);
public void doWait() {
try {
while (ContextLoaderHttpInterceptor.isNotStarted()) {
logger.info("Waiting for ContextLoader to start...");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
logger.error("SpringComponentProvider was interrupted!");
}
}
}
Note that this is specific to *our* code-base as ContextLoaderHttpInterceptor is an http servlet where isNotStarted returns true if our custom ContextLoader (which happens to be asynchronous) is not yet loaded.
The custom asynchronous ContextLoader was put in place sometime by somebody for some reason along the lines of allowing the UI to display a "loading" page while the server boots up. (Probably not the correct way to add this UI "feature", but the code was there and the UI depended on it, so I had to deal with it...)
Since this part will not apply directly to you, the key thing is to debug through SpringComponentProvider (from here) and look at the value of the ClassPathXmlApplicationContext. If it is null, as it is in our case, then you need to figure out why it is null and wait on whatever ContextLoader you use to load before you initialize this component.
Placed this hacky line in SpringComponentProvider --
==>
...
private final ContextLoadWaiter waiter = new ContextLoadWaiter();
...
#Override
public void initialize(ServiceLocator locator) {
waiter.doWait(); // Wait on our asynchronous context loader.
this.locator = locator;
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Context lookup started");
}
...
Created this file: META-INF/services/org.glassfish.jersey.server.spi.ComponentProvider with the contents being the fully qualified classpath to the SpringComponentProvider, e.g. com.company.server.nbi.rest.internal.jspring.SpringComponentProvider
Added the custom Jersey-spring3 package as a package to scan in the application; see below...
==>
/**
* Application configuration.
*
* #author ryan
*
*/
public class MyJerseyApplication extends ResourceConfig {
private static final class Messages {
static final String INF_STARTING_APPLICATION = "Starting %s!";
}
private static final Logger logger = Logger.getLogger(MyJerseyApplication.class);
public MyJerseyApplication() {
packages(
/* Internal providers */
"com.company.server.nbi.rest.providers",
/* Internal filters */
"com.company.server.nbi.rest.filters",
/* Spring injection support */
"com.company.server.nbi.rest.internal.jspring", // HERE!!!
/* Json providers */
"com.fasterxml.jackson.jaxrs.json",
/* Jackson exception mappers */
"com.fasterxml.jackson.jaxrs.base");
/* Resources */
register(ResourceA.class);
register(ResourceB.class);
register(ResourceC.class);
/* Miscellaneous features */
register(MultiPartFeature.class);
register(LoggingFilter.class);
logger.info(format(Messages.INF_STARTING_APPLICATION, this.getClass().getName()));
}
}
That's "it". Definitely not a solution to be proud of, but if you are in desperation mode like I was, it probably doesn't hurt to give it a shot.
This is the message that is key to understanding the issue. It indicates that Spring is failing to initialise correctly:
SEVERE: Spring context lookup failed, skipping spring component provider initialization.
(On a side note: because Spring is failing to initialise, the only JSR-330 implementation to try and resolve the #Inject is HK2 - which is why you're seeing the other issue).
Anyway, the problem is likely that your container isn't performing a scan for the annotations that make all the jersey-spring3 magic happen.
This behaviour is part of the Servlet 3.0 Specification (JSR-33, Section 1.6.2), so you should double check that your container supports this.
In the case of Tomcat - unless you're running Tomcat 7.0.29 or newer, you'll actually need to make sure that the Servlet version is specified in your web.xml.
http://tomcat.apache.org/tomcat-7.0-doc/changelog.html#Tomcat_7.0.29_(markt)
I hit this problem recently and it drove me nuts, and fixing the web.xml was easier than upgrading from Ubuntu/Precise!
Hope this helps!

Resources