Integrating Jersey 2 and Spring with Java Based Configuration - java

I am using Jersey 2.10 and jersey-spring3 and Spring 4.
I want to achieve DI(basically services) in jersey resources as well as in other places and want to create Spring Beans through Java Configuration.
Currently,I am not able to find out any way to do this.
Any idea how to do this?
my web.xml looks like this
<web-app>
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>
jersey.config.server.provider.packages
</param-name>
<param-value>com.xyz</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

web-app:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>xxx.xxx.configuration.ApplicationConfiguration</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>xxx.xxx.controllers.HelloController</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
JavaBased Configuration:
#Configuration
public class ApplicationConfiguration {
#Bean
HelloService helloService () {
return new HelloServiceImpl();
}
}
and simple controller:
#Component
#Path("/helloController")
public class HelloController {
#Autowired
#Qualifier("helloService")
private HelloService helloService ;
#GET
#Path("/hello")
public String hello() {
helloService.service();
}
}
for testing:
localhost:8080/[AppName]/helloController/hello
remember about excluding old Spring dependencies you may have some conflicts if you don't. You can do this same as on the example below or through DependencyManagement.
<dependencies>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.11</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-server</artifactId>
<groupId>org.glassfish.jersey.core</groupId>
</exclusion>
<exclusion>
<artifactId>
jersey-container-servlet-core
</artifactId>
<groupId>org.glassfish.jersey.containers</groupId>
</exclusion>
<exclusion>
<artifactId>hk2</artifactId>
<groupId>org.glassfish.hk2</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
</dependencies>

Old Fashioned way:
Since you have already initialized the ContextLoaderListener a simple trick is to use the WebApplicationContext to retrieve your beans at any application point:
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
SomeBean someBean = (SomeBean) ctx.getBean("someBean");
Jersey Support:
Or you can use the annotation based discovery, since Jersey has already support for Spring DI. You have to register your beans under your main application entry point. That entry point, in below example will be some.package.MyApplication, should be provided as an <init-param> of the servlet container:
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>some.package.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Register you beans in your application:
package some.package;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class MyApplication extends ResourceConfig {
public MyApplication () {
register(RequestContextFilter.class);
register(SomeBean.class);
// ...
}
}
Here you can take a look to a ready to run example from Jersey Git repo.

Here is something that I found starting from various tutorials. Combined with other answers you should have a complete example.
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class WebInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
ServletRegistration.Dynamic servlet = servletContext.addServlet("jersey-serlvet", new SpringServlet());
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}

For those trying to do it with Java config:
public static void main(String[] args) throws IOException {
HttpServer server = new HttpServer();
NetworkListener listener = new NetworkListener("grizzly2", "localhost", 2088);
server.addListener(listener);
WebappContext ctx = new WebappContext("ctx","/");
final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet());
reg.addMapping("/*");
ctx.addContextInitParameter( "contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext" );
ctx.addContextInitParameter( "contextConfigLocation", "com.example.AppConfig" );
ctx.addListener( "org.springframework.web.context.ContextLoaderListener" );
ctx.addListener("org.springframework.web.context.request.RequestContextListener");
ctx.deploy(server);
server.start();
System.in.read();
}

Related

mixing spring and jersy?

I have a web application that works great
and I would like to integrate it with another web application that is rest interface in JSON implemented using Jersey
the spring controllers are using RequestMapping like:
#Controller
public class AdminPrinterController {
#RequestMapping(value = "/contact/view.action")
public #ResponseBody
Map<String, ? extends Object> view() throws Exception {...}
while the Jersey controllers look like?:
#Path("/printerList")
public class PrinterListApi{
#Path("/internalPrinterList/{locationId}")
I integrated the code, but its obviously not working... probably because spring is intercepting the Jersey URL
this is my spring filter mapping:
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Can Jersey live together with spring?
Or should i switch to Spring rest implementation
Thank you
It's possible. You'll have to include the spring-jersey dependency in your project. Then you should be able to use the standard spring servlet mapping. I pulled this out of a pretty old project so you might want to check for updated versions. Mixing jersey and spring is a little messy though. I think the more modern way is to follow a spring boot rest tutorial.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.23</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23</version>
<exclusions>
<exclusion>
<artifactId>jersey-bean-validation</artifactId>
<groupId>org.glassfish.jersey.ext</groupId>
</exclusion>
<exclusion>
<artifactId>bean-validator</artifactId>
<groupId>org.glassfish.hk2.external</groupId>
</exclusion>
</exclusions>
</dependency>
Sorry for the edit. I forgot in your web.xml you'll have to point to the jersey servlet container.
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.application.MainApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Jersey Rest API Documentation using Swagger

I am trying to view the list of my jersey rest service methods in API Documentation using Swagger. Went through few examples/sample given in GitHub sites. But still I am not able to list out my service methods when I try to access the context-root link. Getting 404 service not found.
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.danfoss.des</groupId>
<artifactId>SampleRestProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<!-- <dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency> -->
</dependencies>
<build>
<finalName>SampleRestProject</finalName>
</build>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>io.swagger.jaxrs.listing,com.danfoss.des</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- <init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8081/SampleRestProject/</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8081/SampleRestProject/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> -->
</web-app>
Service java class:
package com.danfoss.des;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.danfoss.model.Track;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
#Path("/helloWorld")
#Api(value="helloWorld", description="Sample hello world swagger service")
public class RESTfulHelloWorld
{
#GET
#Produces("text/html")
#Path("/startingPage")
#ApiOperation(value="Starting of the swagger service")
public Response getStartingPage()
{
String output = "Staring method is invoked";
return Response.status(200).entity(output).build();
}
}
My Project structure
The link am trying to access to view the list: http://localhost:8081/SampleRestProject/api-docs
Can someone please help me find out where exactly am going wrong or if I am missing out anything.
The JerseyJaxrsConfig class is part of the swagger-jersey2-jaxrs library and hence is not available when deploying the webapp, because you're using swagger-jersey-jaxrs combined with Jersey 1.9 (which is good).
Simply replace
<servlet-class>io.swagger.jaxrs.config.JerseyJaxrsConfig</servlet-class>
with
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
to use the correct configuration class and then try accessing http://localhost:8081/SampleRestProject/rest/swagger.json again.
Also while you're at it, consider defining Swagger's basepath as a relative path so you're able to deploy the webapp on different ports.
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/SampleRestProject/rest</param-value>
</init-param>

404 when calling Jersey 2 REST endpoint on Glassfish

This is my web.xml
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Simulator HTTP API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
and this is my simple web service:
#Path("partner")
public class PartnerAPI {
#Path("/mt")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sendMT() {
return "Sent";
}
}
when i call it like this:
http://localhost:8080/myprojectname/partner/mt
i get 404 error mot found, what am i doing wrong?
Update
this is my maven
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
You have different deployment options in Jersey 2:
If you want to do it via web.xml you have to add the an init-param where you specify which packages should be scanned:
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>insert.packagename.where.your.class.is.here</param-value>
</init-param>
</servlet>
Another option is to create a basic class to configure your REST application.
This would look like this:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
#ApplicationPath("/test")
public class YourApplication extends ResourceConfig {
public YourApplication() {
this.packages("insert.packagename.where.your.class.is.here");
}
}
Make sure to update the string with the package name where your PartnerAPI class is.
Then add the value inside #ApplicationPath to your URL.
The link would look like this: http://localhost:8080/myprojectname/test/partner/mt
More information: Jersey docs: Chapter 4. Deploying a RESTful Web Service

Spring Framework MVC 4, JPA, Atmosphere-jersey, Real time Notifications

I need to make push notifications on our webapp. Currently we built the app using Spring Framework MVC in Maven using Spring Tool Suite. All features are done but we need to add this real time notification feature like in facebook using Atmosphere-jersey.
I did a thorough search everywhere in the web before posting this btw. Soooooooo...
MAIN PROBLEM: Spring Framework MVC 4 is separate instance in Atmosphere.(Instance or whatever the term is) So I cant Autowire the service from spring to Atmosphere service. Please give me concepts or ideas on what to do here using this libraries.
There's no problem in the frontend. Just got stucked on this spring and atmosphere thingy.
Tried the answers in this question but didn't work: atmosphere + spring + autowired problems
Approach 1: #Configurable, #Autowired but service is still null.
Searches did:
Using #Configurable which needs enabled LTW(Load Time Weaving), but got stucked when whwre -javaagent has a bug or whatever in vFabric TC Server. - http://forum.spring.io/forum/spring-projects/springsource-tool-suite/116703-vmware-vfabric-tc-server-does-not-save-launch-configuration
#ManagedService(path = "/notification/{channel: [a-zA-Z][a-zA-Z_0-9]*}")
#Singleton
#Configurable(dependencyCheck=true,autowire=Autowire.BY_TYPE)
public final class NotificationChannelController {
private final Logger logger = LoggerFactory.getLogger(NotificationChannelController.class);
private final ConcurrentHashMap<String, String> users = new ConcurrentHashMap<String, String>();
private final static String notifChannel = "/notification/";
#Autowired
private NotificationService notificationService;
#PathParam("channel")
private String channel;
#Inject
private BroadcasterFactory factory;
#Inject
private AtmosphereResourceFactory resourceFactory;
#Inject
private MetaBroadcaster metaBroadcaster;
#Ready(encoders = {NotificationProtocolCodec.class})
#DeliverTo(DeliverTo.DELIVER_TO.ALL)
public void onReady(final AtmosphereResource r) {
logger.info("Browser {} connected.", r.uuid());
return;
}
#Disconnect
public void onDisconnect(AtmosphereResourceEvent event) {
users.values().remove(event.getResource().uuid());
if (event.isCancelled()) {
// We didn't get notified, so we remove the user.
logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
} else if (event.isClosedByClient()) {
logger.info("Browser {} closed the connection", event.getResource().uuid());
}
}
#Message(encoders = {NotificationProtocolCodec.class}, decoders = {NotificationProtocolCodec.class})
public NotificationProtocol onMessage(NotificationProtocol message) throws IOException {
if (!users.containsKey(message.getConciergeId())) {
users.put(message.getConciergeId(), message.getUuid());
}
if (message.getMessage().contains("disconnecting")) {
users.remove(message.getConciergeId());
return new NotificationProtocol(message.getConciergeId(), "", "");//" disconnected from room " + channel);
}
logger.info("{} just send {}", message.getConciergeId(), message.getMessage());
return new NotificationProtocol(message);
}
#Message(encoders = {NotificationProtocolCodec.class}, decoders = {NotificationCodec.class})
public void onPrivateNotification(NotificationComment notificationComment) throws IOException {
List<NotificationProtocol> npList = notificationService.saveAndNotifyUsers(notificationComment);
for(NotificationProtocol np : npList){
if (userUUID != null) {
// Retrieve the original AtmosphereResource
AtmosphereResource r = resourceFactory.find(userUUID);
if (r != null) {
factory.lookup(notifChannel + channel).broadcast(np, r);
}
}
}
}
Service to Autowire
#Service
#Transactional
public class NotificationServiceImpl implements NotificationService{
}
web.xml
<!-- Atmosphere -->
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterLifeCyclePolicy</param-name>
<param-value>EMPTY_DESTROY</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcaster.shareableThreadPool</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.recoverFromDestroyedBroadcaster</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.websocket.messageContentType</param-name>
<param-value>application/json</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/notification/*</url-pattern>
</servlet-mapping>
application-context.xml
<context:component-scan base-package="packagefolder" />
<context:annotation-config />
<context:load-time-weaver aspectj-weaving="on"/>
pom.xml
<!-- Atmosphere -->
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-jersey</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.atmosphere.client</groupId>
<artifactId>javascript</artifactId>
<version>2.2.4</version>
<type>war</type>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<!-- Spring ORM Hibernate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- #Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
Just say so if you want more of the pom.xml
Tried this but no good
-javaagent:"pathto/spring-instrument-4.0.6.RELEASE.jar"
Approach 2: For now, trying to get the users connected to NotificationChannelController from a spring controller. But still null even if clients conneted to the managedservice path when accessing from spring controller. I know its not the right way but have to do some tricks to make this work.
Controller:
#Controller
#RequestMapping("secured/*")
public class BaseController {
private static final Logger LOGGER = LoggerFactory
.getLogger(BaseController.class);
#RequestMapping(value = "test", method = RequestMethod.GET)
#ResponseBody
public void testMethod() throws Exception {
System.out.println("AAAAAAAAAAAAAAAAAA");
NotificationChannelController ncc = new NotificationChannelController();
ncc.show();
}
}
NotificationChannelController
#ManagedService(path = "/notification/{channel: [a-zA-Z][a-zA-Z_0-9]*}")
#Singleton
#Configurable(preConstruction=true,dependencyCheck=true,autowire=Autowire.BY_TYPE)
public final class NotificationChannelController {
private final Logger logger = LoggerFactory.getLogger(NotificationChannelController.class);
private final ConcurrentHashMap<String, String> users = new ConcurrentHashMap<String, String>();
public void show(){
System.out.println("A");
for(String key : users.keySet()){
System.out.println(key + " " + users.get(key));
}
System.out.println("B");
for(String s : getRooms(factory.lookupAll())){
System.out.println(s);
}
System.out.println("C");
}
}
Thanks for taking time on reading this and I hope someone can shed light on this. Thanks!
did you read https://github.com/Atmosphere/atmosphere/wiki/Configuring-Atmosphere's-Classes-Creation-and-Injection You needs atmosphere-spring extension if you want to make it work. Take a look a the code here
https://github.com/Atmosphere/atmosphere-extensions/tree/master/spring/modules/src/main/java/org/atmosphere/spring
-- Jeanfrancois
has been a while since implementing Java but don't you need some kind of bean declaration as configuration to get a been autowired?
In Example for a cxf tutorial i autowired a Dao to a service the bean itself is defined in the beans.xml.
maybe you can have a look and good luck!

Target unreachable, Identifier resolved to null with correct names, dependencys, imports

I've got a strange issue concerning the usage of an el-tag in JSF. I have an nearly empty project with the following dependencies:
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>cupertino</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
And I have an bean, which starts like this:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
#ManagedBean(name = "requester")
#ApplicationScoped
public class UserBean implements Serializable {
public String getRequestIdentificator() {
return requestIdentificator;
}
public void setRequestIdentificator(String requestIdentificator) {
this.requestIdentificator = requestIdentificator;
}
and web.xml looks like this:
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="DAS_ID">
<display-name>DAS</display-name>
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<description>Parameter required by Mojarra 2.0</description>
<param-name>com.sun.faces.allowTextChildren</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
Tell the runtime where we are in the project development
lifecycle. Valid values are:
Development, UnitTest, SystemTest, or Production.
The runtime will display helpful hints to correct common mistakes
when the value is Development.
</description>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/main.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Unfortunately, everytime, something like <p:commandButton value="Request" action="#{requester.requestData}" /> is called, this results in
SEVERE: javax.el.PropertyNotFoundException: /main.xhtml #19,100 value="#{requester.requestIdentificator}": Target Unreachable, identifier 'requester'
resolved to null
I already searched for errors, the common errors are e.g. summarized here: Target Unreachable, identifier resolved to null, but none of this applies. Furthermore, NetBeans is able to use code-completion for everything I am doing (I also tried it with other things than requestIdentificator) - so it seems like code is written fully correct. The Bean seams never to be initialized (so the logging in the constructor is not called), but according to e.g. https://stackoverflow.com/a/11013290/2096209, is is not necessary to register the Bean in faces-config in JSF 2.x (I am using 2.1), so that can not be the problem.
I am running everything with mvn tomcat7:run. Has anybody an hint how to solve this?
I Often have this same problem using the javax.faces.bean package. Try importing
import javax.enterprise.context.ApplicationScoped;
instead of
import javax.faces.bean.ApplicationScoped;
And instead of the #ManagedBean annotation use #Named
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
#Named("requester")
#ApplicationScoped
public class UserBean implements Serializable {
Then build and redeploy. See if that helps
The problem was that I tried running it with mvn tomcat7:run. If I copy the war into a normal tomcat7-instance, it works, and as well, if I run it with mvn tomcat7:run-war. Seems to be an maven-tomcat-plugin bug.

Categories