Apache OpenWebBeans(CDI) + Servlet, injection not working - apache

I am trying to get CDI working in tomcat 9.x. I followed the following links but still openwebbeans container did not inject the resource into the servlet
https://devlearnings.wordpress.com/2011/05/15/apache-openwebbeans-cdi-from-standalone-to-webapp/
https://dzone.com/articles/using-apache-openwebbeans
http://openwebbeans.apache.org/owbsetup_ee.html
Below is my servlet
package com.openwebbeans;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SampleController extends HttpServlet{
private static final long serialVersionUID = 1L;
#Inject
private SampleService service;
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
response.getWriter().print(service);
}
}
Below is the web.xml
<web-app 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"
version="3.0">
<display-name>OpenWebBeans</display-name>
<listener>
<listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>com.openwebbeans.SampleController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
</web-app>
Added the below line in server.xml
<Listener className="org.apache.webbeans.web.tomcat7.ContextLifecycleListener" />
Below is my 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.openwebbeans</groupId>
<artifactId>openwebbeans-beginner</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>openwebbeans-beginner</name>
<url>http://maven.apache.org</url>
<properties>
<owb.version>2.0.0</owb.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_2.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.3_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-web</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-tomcat7</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>openwebbeans-beginner</finalName>
</build>
</project>
Added the below jars under tomcat lib
geronimo-annotation_1.3_spec-1.0
geronimo-atinject_1.0_spec-1.0
geronimo-interceptor_1.2_spec-1.0
geronimo-jcdi_2.0_spec-1.0
openwebbeans-el22-2.0.0
openwebbeans-impl-2.0.0
openwebbeans-spi-2.0.0
openwebbeans-tomcat7-2.0.0
openwebbeans-web-2.0.0
xbean-asm5-shaded-4.5
xbean-finder-shaded-4.5
javassist-3.21.0-GA
Below are the server logs after deploying my war. It is clear from the logs that open web beans container has started
20-Jul-2017 10:06:08.315 INFO [http-nio-8080-exec-5]
org.apache.catalina.startup.HostConfig.deployWAR Deploying web
application archive
[D:\krishna\apache-tomcat-9.0.0.M22\webapps\openwebbeans-beginner.war]
20-Jul-2017 10:06:08.904 INFO [http-nio-8080-exec-5]
org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication
OpenWebBeans Container is starting... 20-Jul-2017 10:06:09.229 INFO
[http-nio-8080-exec-5]
org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication
OpenWebBeans Container has started, it took [325] ms. 20-Jul-2017
10:06:09.235 INFO [http-nio-8080-exec-5]
org.apache.catalina.startup.HostConfig.deployWAR Deployment of web
application archive
[D:\krishna\apache-tomcat-9.0.0.M22\webapps\openwebbeans-beginner.war]
has finished in [920] ms
Additionally i created openwebbeans.properties under META-INF/openwebbeans and added org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.lifecycle.StandaloneLifeCycle to it. But it still does not work
I also tried with org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.web.lifecycle.WebContainerLifecycle but nothing seems to work.
Can anyone please help me get this working?

If you need injection in servlets then you need a deeper integration than just the servlet listener. We provide this with the openwebbeans-tomcat7 plugin.
The easiest way is to use our installer as explained in our announce mail
https://lists.apache.org/thread.html/15b8cbcdbcc24942dae6d277d75363103a9d45d59047fda0e6abcbbe#%3Cannounce.apache.org%3E
In that case just remove the whole WebBeansConfigurationListener from your web.xml. This is intended if you like to integrate in 'unpimped' servlet containers or if you run in GAE, etc. In which case you can work around with using CDI.current() to get your CDI bean in a Servlet.
You also don't need any javassist dependency anymore. That got removed in OWB-1.2.x a long time ago.
Feel free to ping us on our mailing lists or irc at #openwebbeans on freenode.net!
Oh and another tip: you could try out our Apache Meecrowave container, which is OWB + Tomcat9 + CXF + Johnzon - all in 9MB. It includes a maven-plugin, an Arquillian container, etc.

Related

Spring + RESTEasy subresource is not injected with the dependencies

I'm using Spring for IoC and Data JPA support and JAX-RS support is provided by RESTEasy implementation.
I have the following pom.xml dependencies:
<dependencies>
<!-- Database Dependencies -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Integration with RESTEasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>4.2.0.Final</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>4.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>4.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>4.4.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies>
web.xml :
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
</web-app>
appContext.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<context:component-scan base-package="org.test" />
</beans:beans>
I have two resources:
Parent:
#Component
#Path("/parent")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class ParentResource {
#Context
ResourceContext resourceContext;
#GET
#Path("sub")
public ChildResource getSub() {
return resourceContext.getResource(ChildResource.class);
}
}
Child:
#Component
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class ChildResource {
#Autowired
Repository1 repository1;
}
This example is totally working with Jersey implementation and I can get repository from the sub resource, but in RESTEasy implementation it does not work and instead of repository instance there is null.
There is a workaround, like this:
ChildResource resource = resourceContext.getResource(ChildResource.class);
resource.setRep(repositoryFromParent);
return resource;
But it is ugly and it should not be in that way (also parent should know about all dependencies of the sub resource which is also not good).
Has anyone encountered the same issue working with subresources using Spring and RESTEasy together?

Error In Cucumber Extend report(java.lang.ClassNotFoundException: com.cucumber.listener.ExtentCucumberFormatter)

I am new in selenium.
I want to generate extend report using cucumber. But I am always getting error
"cucumber.runtime.CucumberException: Couldn't load plugin class: com.cucumber.listener.ExtentCucumberFormatter"
I tried every version Below code I have
POM XML File:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberFramwork</groupId>
<artifactId>ExtendReport</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ExtendReport</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-cucumber4-adapter</artifactId>
<version>1.0.7</version>
</dependency>
<!-- >dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Java Code:
package runners;
import java.io.File;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import com.vimalselvam.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = "src/test/resources/functionalTests",
glue = { "stepDefinitions" }, plugin = {
"com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html" },
monochrome = true)
public class TestRunner {
#AfterClass
public static void writeExtentReport() {
try {
Reporter.loadXMLConfig(new File("C:\\Software\\WprkSpace\\ExtendReport\\Configs\\extent-config.xml"));
}
catch (Exception e) {
e.printStackTrace();
}
//Reporter.loadXMLConfig("/ExtendReport/Configs/extent-config.xml");
}
}
Every time I am geting below error:
cucumber.runtime.CucumberException: Couldn't load plugin class:
com.cucumber.listener.ExtentCucumberFormatter
at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:180)
at cucumber.runtime.formatter.PluginFactory.pluginClass(PluginFactory.java:165)
at cucumber.runtime.formatter.PluginFactory.getPluginClass(PluginFactory.java:222)
at cucumber.runtime.formatter.PluginFactory.isStepDefinitionReporterName(PluginFactory.java:205)
at cucumber.runtime.RuntimeOptions$ParsedPluginData.addPluginName(RuntimeOptions.java:357)
at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:159)
at cucumber.runtime.RuntimeOptions.(RuntimeOptions.java:90)
at cucumber.runtime.RuntimeOptions.(RuntimeOptions.java:85)
at cucumber.runtime.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:23)
at cucumber.api.junit.Cucumber.(Cucumber.java:84)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: com.cucumber.listener.ExtentCucumberFormatter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:173)
... 26 more
Problem Statement Analysis -
Using vimal, avenstack and adapter dependencies all together in pom.xml which is not advisable and expected. One shall use vimal or avenstack or both together in specific use case or use adapter dependency only. Please follow below instructions.
Adding direct & transitive dependencies together from info.cukes & io.cucumber dependencies. Doing so can cause unpredictable outcome. Now let's move.
Vimal Selvam Library: Below is the required maven dependency and a sample test to demonstrate how configuration set up is done.
Maven Dependency
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>
Please note that Java 8+ and adding the dependency of ExtentReport v3.1.1+ is mandatory.
Cucumber Runner File
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/test/resources/features"},
glue = {"com.cucumber.stepdefinitions"},
plugin = {"com.cucumber.listener.ExtentCucumberFormatter:output/report.html"}
)
public class RunCukesTest {
#AfterClass
public static void teardown() {
Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
Reporter.setSystemInfo("user", System.getProperty("user.name"));
Reporter.setSystemInfo("os", "Mac OSX");
Reporter.setTestRunnerOutput("Sample test runner output message");
}
}
The above setup will generate the report in output directory with the name of report.html.
Please remove adapter dependency from pom.xml. We shall use vimal/avenstack or extent adapter but not all together.
Extent Adapter: Beauty is, you do not need to write any code any where to generate report this way except from setting adapter in runner below.
Maven Dependency
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-cucumber4-adapter</artifactId>
<version>1.0.6</version>
</dependency>
Add the com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter plugin to the runner.
#RunWith(Cucumber.class)
#CucumberOptions(plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"})
public class RunCukesTest {
// ..
}
Report Output Directory - ../Project Directory/test-output/HtmlReport
Additional Note: In future, we would request you to use Cucumber v>=4.0.0 as you are using pretty old dependency(v1.2.5) of Cucumber.
For doing so, you can add below set of cucumber minimal dependencies.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
Since you are using cucumber 4 version, you should be using the plugin - "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"
cucumber-java & cucumber-junit dependency should be 4.2.6 version on your POM.
see adapter documentation- http://extentreports.com/docs/versions/4/java/cucumber4.html for more details & examples.

JAX-WS Cannot initialize endpoint... arg0 is not a valid property on class

I have a very strange problem.
Previously, for bigger projects we used EAR packaging (one EJB + JPA + JAX-WS project with one JSF + JAX-RS project).
No we have a smaller project, where we only needed some (5) services and no UI, so we decided to go with WAR packaging in one project (EJB + JPA + JAX-WS + JAX-RS).
Maven dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>${moxy.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jsonp-jaxrs</artifactId>
<version>1.0</version>
</dependency>
So, most of what we do is we create a simple interface:
#WebService
#SOAPBinding( style = Style.DOCUMENT )
public interface ILog extends Serializable{
#WebMethod public long createEventLog(EventLog log);
}
We put an EJB over it, for implementation:
#Stateless
#LocalBean
#WebService (endpointInterface = "....interfaces.ILog")
public class LogService implements ILog {
public long createEventLog(EventLog log) {
em.persist(log);
em.flush();
return log.getId();
}
}
Then we have a JAX-RS class over it, which calls the upper EJB method.
#Path("/log")...
First we create the EJB + WS combos, and create simple test client codes, to make sure they work:
private URL url;
private QName qname;
private Service wsService;
private ILog logService;
#Before
public void init() throws MalformedURLException{
url = new URL("http://localhost:8080/LogServiceService/LogService?wsdl");
qname = new QName("http://.../","LogServiceService");
wsService = Service.create(url, qname);
logService = wsService.getPort(ILog.class);
}
Everything is rendered / generated for us by glassfish 4.1, so we don't create any more configuration class, or anything (the above code just works...).
So this works at this step. After these, we introduced the JAX-RS services (with JSON formatting), that meant the REST services are working, but now I think the above API-s doesn't like each other very much, and now the JAX-WS code isn't working, and giving the following error for all the JAX-WS services:
2015-09-18T14:15:36.712+0200|Severe: Cannot initialize endpoint : error is :
javax.xml.ws.WebServiceException: class ....interfaces.jaxws.CreateEventLog do not have a property of the name arg0
at com.sun.xml.ws.server.sei.EndpointArgumentsBuilder$DocLit.<init>(EndpointArgumentsBuilder.java:610)
at com.sun.xml.ws.server.sei.TieHandler.createArgumentsBuilder(TieHandler.java:143)
at com.sun.xml.ws.server.sei.TieHandler.<init>(TieHandler.java:115)
at com.sun.xml.ws.db.DatabindingImpl.<init>(DatabindingImpl.java:116)
at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:74)
at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:58)
at com.sun.xml.ws.db.DatabindingFactoryImpl.createRuntime(DatabindingFactoryImpl.java:120)
at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:521)
at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:300)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:164)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:577)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:560)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:639)
at org.glassfish.webservices.EjbRuntimeEndpointInfo.prepareInvocation(EjbRuntimeEndpointInfo.java:275)
at org.glassfish.webservices.EjbRuntimeEndpointInfo.initRuntimeInfo(EjbRuntimeEndpointInfo.java:352)
at org.glassfish.webservices.WebServiceEjbEndpointRegistry.registerEndpoint(WebServiceEjbEndpointRegistry.java:127)
at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1247)
at com.sun.ejb.containers.StatelessSessionContainer.initializeHome(StatelessSessionContainer.java:190)
at com.sun.ejb.containers.StatelessContainerFactory.createContainer(StatelessContainerFactory.java:63)
at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:221)
at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:291)
at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:99)
at org.glassfish.internal.data.ModuleInfo.load(ModuleInfo.java:206)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:313)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:496)
at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:406)
at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:243)
at org.jvnet.hk2.internal.ClazzCreator.postConstructMe(ClazzCreator.java:329)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:377)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:461)
at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:227)
at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:84)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2258)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: javax.xml.bind.JAXBException: arg0 is not a valid property on class ....interfaces.jaxws.CreateEventLog
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getElementPropertyAccessor(JAXBContextImpl.java:969)
at com.sun.xml.ws.db.glassfish.JAXBRIContextWrapper.getElementPropertyAccessor(JAXBRIContextWrapper.java:120)
at com.sun.xml.ws.server.sei.EndpointArgumentsBuilder$DocLit.<init>(EndpointArgumentsBuilder.java:598)
... 39 more
Now I don't know what could I do to make the two types of Services work together (I wouldn't like to go back to EAR packaging with two projects, this service interface is way too small for that).
Can anyone help me?
Thank You!

Arquillian, tomee-embedded and JAX-RS

I am using Arquillian with a tomee-embedded container in order to test my JAX-RS web service. In my test case, I am running a jersey test client accessing the provided resource. However, the test always results in a 404 NOT FOUND status when accessing the resource.
When deploying the same project on my tomee-jaxrs instance, the resource is provided properly.
These are the maven dependencies that I've included in my test project:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
My test case looks as follows:
#RunWith(Arquillian.class)
public class DemoTest {
#Deployment
public static WebArchive createDeployment() throws Exception {
return ShrinkWrap.create(WebArchive.class).addPackage(Controller.class.getPackage()).setWebXML("ch/codenation/test/regression/resources/container/WEB-INF/web.xml");
}
#ArquillianResource
private URL url;
#Test
public void testGetData() throws Exception {
final IApplicationLayer applicationLayer = new JaxRsApplicationLayer(url.toURI());
final Callable<String> dataProvider = new DataProvider(applicationLayer);
Assert.assertEquals("asdf", dateProvider.call());
}
}
As well as my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>codenation-service</display-name>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
The JAX-RS resource is indeed unavailable, for when I put a breakpoint in the test method and try to access the resource in the browser, I receive a 404 message. Doing the same thing when deplyoing the archive to a tomee-jaxrs server works fine, however. Are there any additional maven dependencies or arquillian configuration settings I need to add here in order to make this work?
Thanks for any feedback and best regards
Pascal
Just include this in your pom.xml
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-jaxrs</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
and you can work with the embedded TomEE.
There seems to be no way to switch the tomee-embedded adapter to a plus configuration. I therefore switched over to the tomee-remote adapter.

how to test an REST app (built with apache-cxf) with jersey-test-framework

I would like to know how I can test an REST application, which is developed using Apache CXF and Spring, with the Jersey Test Framework (JTF).
The application is made of several Maven modules, of which "app-rest" is the one that integrates them and exposes the REST interface to be tested.
I've made a separate Maven module that contains the tests, which has the "app-rest" as dependency, but I'm receiving an exception like this :
BeanDefinitionStoreException: IOException parsing XML document from class path resource
when running the tests.
I think that's because the app-rest is not properly deployed in the embedded container.
I've tried to put the tests into the "app-rest" module, but I get this instead :
RuntimeException: The scope of the component class org.apache.cxf.jaxrs.provider.AtomFeedProvider must be a singleton
I'm running the test with this command line:
mvn test -Dtest=JerseyRestTest -Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory
These is my pom.xml file for the tests module:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>project-group-id</groupId>
<artifactId>projectname</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>project-group-id</groupId>
<artifactId>integration-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Integration Tests of REST interface</name>
<!-- build -->
<build>
<finalName>${project.artifactId}</finalName>
</build>
<!-- additional repositories -->
<repositories>
<repository>
<id>java.net.m2</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>project-group-id</groupId>
<artifactId>app-rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Jersey stuff -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The simple test is:
public class JerseyRestTest extends JerseyTest {
public JerseyRestTest() {
super(new WebAppDescriptor.Builder("projectname.resource")
.servletClass(SpringServlet.class)
.contextParam("contextConfigLocation", "classpath:META-INF/spring/context-rest.xml")
.contextListenerClass(org.springframework.web.context.ContextLoaderListener.class)
.contextPath("app-rest")
.build());
}
#Test
public void testSomeResource() {
String response = resource().path("/rest/resources").get(String.class);
Assert.assertNotNull("No text returned!", response);
assertResponseContains(response, "<html>");
assertResponseContains(response, "</html>");
}
protected void assertResponseContains(String response, String text) {
Assert.assertTrue("Response should contain " + text + " but was: " + response, response.contains(text));
}
}
Thank you very much!