FindAnnotation class is missing when using resteasy-jackson-provider in jetty application - java

I am trying to use RestEasy in a Jetty application. I am using Jackson for serializing/deserializing my POJOs. I need my serialization to be polymorphic since I have endpoints that handle a POJO type that has sub-types. I thought I could use resteasy-jackson-provider to provide my MessageBodyWriter and MessageBodyReader implementations.
When I call an endpoint I get the following error:
java.lang.NoClassDefFoundError: org/jboss/resteasy/util/FindAnnotation
at org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider.isWriteable(ResteasyJacksonProvider.java:42)
at org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl.resolveMessageBodyWriter(ResteasyProviderFactoryImpl.java:1322)
at org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl.getMessageBodyWriter(ResteasyProviderFactoryImpl.java:1293)
at org.jboss.resteasy.core.ServerResponseWriter.lambda$writeNomapResponse$2(ServerResponseWriter.java:112)
at org.jboss.resteasy.core.interception.jaxrs.ContainerResponseContextImpl.filter(ContainerResponseContextImpl.java:405)
at org.jboss.resteasy.core.ServerResponseWriter.executeFilters(ServerResponseWriter.java:232)
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:97)
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:70)
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:578)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:508)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:249)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:60)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:561)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:334)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597)
at java.base/java.lang.Thread.run(Thread.java:834)
Here's my POM file:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.foster.app</groupId>
<artifactId>jettyRestEasyApiService</artifactId>
<version>3.0.0</version>
<packaging>jar</packaging>
<name>Jetty-RESTEasy Api Service</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.jetty>9.4.7.v20170914</version.jetty>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${version.jetty}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-core</artifactId>
<version>4.2.0.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.9.0.Final</version>
</dependency>
</dependencies>
<build>
<finalName>JettyRestEasyApiService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.foster.app.resteasy.rest.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Here's my Main class:
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
public class App {
private static final Logger LOGGER = Logger.getLogger(App.class.getName());
private static final String APPLICATION_PATH = "/jetty/resteasy";
private static final String CONTEXT_ROOT = "/";
private static final Server JETTY_SERVER = new Server(8080);
public App() {}
public static void main(String[] args) throws Exception {
try {
new App().run();
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Application Exception", ex);
throw ex;
} finally {
if (JETTY_SERVER != null) {
JETTY_SERVER.destroy();
}
}
}
public void run() throws Exception {
final ServletContextHandler context = new ServletContextHandler(JETTY_SERVER, CONTEXT_ROOT);
final ServletHolder restEasyServlet = new ServletHolder(new HttpServletDispatcher());
restEasyServlet.setInitParameter("resteasy.servlet.mapping.prefix", APPLICATION_PATH);
restEasyServlet.setInitParameter("javax.ws.rs.Application", JaxRsActivator.class.getCanonicalName());
context.addServlet(restEasyServlet, APPLICATION_PATH + "/*");
final ServletHolder defaultServlet = new ServletHolder(new DefaultServlet());
context.addServlet(defaultServlet, CONTEXT_ROOT);
JETTY_SERVER.start();
LOGGER.log(Level.INFO, "Started Server");
JETTY_SERVER.join();
LOGGER.log(Level.INFO, "Joined Server");
}
}
Here's my Application class:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class JaxRsActivator extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
resources.add(MessageResource.class);
resources.add(TestResource.class);
return resources;
}
}
Here are my POJO classes:
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.io.Serializable;
import java.util.Date;
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "$type")
#JsonSubTypes({
#JsonSubTypes.Type(
value = PojoDog.class,
name = "PojoDog"),
#JsonSubTypes.Type(
value = PojoCat.class,
name = "PojoCat")
})
public abstract class Pojo implements Serializable {
public String property1;
public String property2;
public int property3;
public Date property4;
}
public class PojoCat extends Pojo {
public long sebastian;
}
public class PojoDog extends Pojo {
public String clifford;
}
Here's my endpoint:
#Path("/test")
public class TestResource {
#GET
#Path("pojo")
#Produces(MediaType.APPLICATION_JSON)
public Response getPojo() {
Pojo pojo = new PojoDog();
pojo.property1 = "Hello";
pojo.property2 = "World";
pojo.property3 = 5;
pojo.property4 = new Date();
((PojoDog)pojo).clifford = "I am a dog.";
return Response.ok(pojo).build();
}
}

The class org.jboss.resteasy.util.FindAnnotation is part of the resteasy-jaxrs artifact.
See search:
https://search.maven.org/search?q=fc:org.jboss.resteasy.util.FindAnnotation
That pointed me to see that you have mixed resteasy versions.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-core</artifactId>
<version>4.2.0.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.9.0.Final</version>
</dependency>
That's 100% not valid, you need to use the same resteasy version everywhere.
So from that, I took your code and made a few simple changes, and it worked.
I upgraded to Jetty 9.4.20.v20190813.
Upgraded to org.jboss.resteasy/resteasy-jackson2-provider/4.2.0.Final
Removed the two Jackson dependencies from the pom entirely, relied on them coming from transitive dependency on resteasy-jackson2-provider
<!-- REMOVED : no point having them here
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
-->
I initialized the ServletContextHandler slightly differently (don't pass server into constructor, as that's the job of the handler tree).
final ServletContextHandler context = new ServletContextHandler();
final ServletHolder restEasyServlet = new ServletHolder(new HttpServletDispatcher());
context.setContextPath(CONTEXT_ROOT);
restEasyServlet.setInitParameter("resteasy.servlet.mapping.prefix", APPLICATION_PATH);
restEasyServlet.setInitParameter("javax.ws.rs.Application", JaxRsActivator.class.getCanonicalName());
context.addServlet(restEasyServlet, APPLICATION_PATH + "/*");
final ServletHolder defaultServlet = new ServletHolder("default", DefaultServlet.class); // the name "default" here is important
context.addServlet(defaultServlet, "/"); // this is not context-root, it's the default url-pattern
HandlerList handlers = new HandlerList();
handlers.addHandler(context);
handlers.addHandler(new DefaultHandler()); // to report errors that don't match the root context better
JETTY_SERVER.setHandler(handlers);
JETTY_SERVER.start();

Related

Spring JMS MessageConversionException Cannot convert object to type to JMS Message

I'm creating a Publisher Spring boot application to test sending an Account object using jmsTemplate.convertAndSend(destination, account); to a Consumer Spring boot application I created.
In my pom.xml I have the following <dependencies> and <plugin> setup which lets maven-jaxb2-plugin generate the Java object from the xsd folder I created.
pom.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- user-added -->
<plugin>
<!-- https://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin -->
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
</build>
MessageProducerApplication
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.core.JmsTemplate;
import com.example.xmlns.Account;
#SpringBootApplication
public class MessageProducerApplication implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageProducerApplication.class.getName());
#Autowired
private JmsTemplate jmsTemplate;
#Value("${outbound.queue}")
private String destination; // outbound queue
public static void main(String[] args) {
SpringApplication.run(MessageProducerApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
ClassPathResource xmlResource = new ClassPathResource("xml/accountcreate.xml");
InputStream xmlStream = ClassLoader.getSystemResourceAsStream(xmlResource.getPath());
File xmlFile = new File("src/main/resources/xml/accountcreate.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Account account = (Account) jaxbUnmarshaller.unmarshal(xmlFile);
//Account account = (Account) jaxbUnmarshaller.unmarshal(xmlStream);
jmsTemplate.convertAndSend(destination, account);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Problem
The conversion to Account type fails in this line:
Account account = (Account) jaxbUnmarshaller.unmarshal(xmlFile);
Exception
org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [com.example.xmlns.Account] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.
at org.springframework.jms.support.converter.SimpleMessageConverter.toMessage(SimpleMessageConverter.java:79) ~[spring-jms-5.3.19.jar:5.3.19]
The Account.java class was generated by xjc (both my Producer and Consumer application has the same Account class)
#XmlRootElement(name = "account")
public class Account {
#XmlElement(required = true)
//fields....
}
In my Consumer application, I set the listener to receive a type of Account
#Component
public class MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageListener.class);
#JmsListener(destination = "${inbound.queue}")
public void onMessage(Account account) {
LOGGER.info("account"+account);
}
}
I'd appreciate any thoughts or suggestion. Thanks.
Okay, after reviewing my code I found the reason why it's failing to cast to Account after unmarshal(). I realized, I wasn't using the Jaxb2Marshaller that I configured with a MarshallingMessageConverter in my #Configuration class.
The changes to be made on MessageProducerApplication class are as follows
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
#Autowired
private Jaxb2Marshaller jaxb2Marshaller; //inject this to perform unmarshalling based on configuration in JmsConfig class
File xmlFile = xmlResource.getFile();
try {
Account account = (Account) jaxb2Marshaller.unmarshal(new StreamSource(xmlFile));
jmsTemplate.convertAndSend(destination, account);
} catch (Exception e) {
e.printStackTrace();
}
Then add the configuration class
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.Marshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.support.converter.MarshallingMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
#Configuration
public class JmsConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JmsConfig.class.getName());
#Bean
public Jaxb2Marshaller createJaxb2Marshaller(#Value("${javaobject.ctxpath}") final String contextPath) {
ClassPathResource resource = new ClassPathResource("xsd/account.xsd");
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath(contextPath);
jaxb2Marshaller.setSchema(resource);
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);// pretty print the xml
jaxb2Marshaller.setMarshallerProperties(properties);
return jaxb2Marshaller;
}
#Bean
public MarshallingMessageConverter createMashallingMessageConverter(final Jaxb2Marshaller jaxb2Marshaller) {
return new MarshallingMessageConverter(jaxb2Marshaller);
}
}
I hope this would help others who might encounter the same problem.

Basic Spring based WSSecurity client gets a null response

I've been following this tutorial to learn how to develop a basic spring client and server application using wssecurity (certificates).
The demo works beautifully, but i need to deploy my application on a wildfly server, so i had to change the example a bit in order to avoid the embedded tomcat, the changes are as follows:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.memorynotfound.spring.ws</groupId>
<artifactId>ws-security-certificate-wss4j-security-interceptor</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>SPRING-WS - ${project.artifactId}</name>
<url>http://memorynotfound.com</url>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>1.6.19</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>src/main/resources/xsd</source>
</sources>
<generatePackage>com.memorynotfound.beer</generatePackage>
</configuration>
</plugin>
</plugins>
</build>
RunServer.java
package com.memorynotfound.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class RunServer extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RunServer.class);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<RunServer> applicationClass = RunServer.class;
}
BeerEndpoint.java
package it.corvallis.soap.endpoint;
import com.memorynotfound.beer.Beer;
import com.memorynotfound.beer.GetBeerRequest;
import com.memorynotfound.beer.GetBeerResponse;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.ws.soap.server.endpoint.annotation.SoapAction;
#Endpoint
public class BeerEndpoint {
public static final String NAMESPACE_URI = "http://memorynotfound.com/beer";
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "getBeerRequest")
#SoapAction("http://localhost:8080/ws/beers")
#ResponsePayload
public GetBeerResponse getBeer(#RequestPayload GetBeerRequest request) {
GetBeerResponse beerResponse = new GetBeerResponse();
Beer beer = new Beer();
beer.setId(request.getId());
beer.setName("Beer name");
beerResponse.setBeer(beer);
return beerResponse;
}
}
I deployed the server application on my wildfly and i tried to call it using the same client application used in the tutorial. The service seems to answer correctly, the beerResponse object is correctly istantiated, but, as soon as the object is being sent back to the the client, i see it's NULL.
What am i missing?
Thanks in advance
Oh well, next time i'll read better. At the same link of my first post, a guy asked the same question in the comments, here is the solution: add a callBackHandler to the client configuration, and modify the securityInterceptor to use this handler.
SoapClientConfig.java
package com.memorynotfound.client;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor;
import org.springframework.ws.soap.security.wss4j2.callback.KeyStoreCallbackHandler;
import org.springframework.ws.soap.security.wss4j2.support.CryptoFactoryBean;
import java.io.IOException;
#Configuration
public class SoapClientConfig {
#Bean
public Wss4jSecurityInterceptor securityInterceptor() throws Exception {
Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
// set security actions
securityInterceptor.setSecurementActions("Timestamp Signature Encrypt");
// sign the request
securityInterceptor.setSecurementUsername("client");
securityInterceptor.setSecurementPassword("changeit");
securityInterceptor.setSecurementSignatureCrypto(getCryptoFactoryBean().getObject());
// encrypt the request
securityInterceptor.setSecurementEncryptionUser("server-public");
securityInterceptor.setSecurementEncryptionCrypto(getCryptoFactoryBean().getObject());
securityInterceptor.setSecurementEncryptionParts("{Content}{http://memorynotfound.com/beer}getBeerRequest");
// sign the response
securityInterceptor.setValidationActions("Signature Encrypt");
securityInterceptor.setValidationSignatureCrypto(getCryptoFactoryBean().getObject());
securityInterceptor.setValidationDecryptionCrypto(getCryptoFactoryBean().getObject());
securityInterceptor.setValidationCallbackHandler(securityCallbackHandler());
return securityInterceptor;
}
#Bean
public CryptoFactoryBean getCryptoFactoryBean() throws IOException {
CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
cryptoFactoryBean.setKeyStorePassword("changeit");
cryptoFactoryBean.setKeyStoreLocation(new ClassPathResource("client.jks"));
return cryptoFactoryBean;
}
#Bean
public Jaxb2Marshaller getMarshaller(){
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.memorynotfound.beer");
return marshaller;
}
#Bean
public BeerClient getBeerClient() throws Exception {
BeerClient beerClient = new BeerClient();
beerClient.setMarshaller(getMarshaller());
beerClient.setUnmarshaller(getMarshaller());
beerClient.setDefaultUri("http://localhost:8080/ws/beers");
ClientInterceptor[] interceptors = new ClientInterceptor[]{securityInterceptor()};
beerClient.setInterceptors(interceptors);
return beerClient;
}
#Bean
public KeyStoreCallbackHandler securityCallbackHandler(){
KeyStoreCallbackHandler callbackHandler = new KeyStoreCallbackHandler();
callbackHandler.setPrivateKeyPassword("changeit");
return callbackHandler;
}
}

How to register an object in jersey 2(ResourceConfig)

I want to register a class component(Object) in jersey not class reference.
I create a XYZResource class that be called by main() method and it be configured through jersey with DBI object. DAOService daoService has connection DBI connection and the getDetails() that execute query and return result.
I use IntelliJ IDE and run through Main Class com.example.Main
when i hit my url http://localhost:9000/resource/test than it through error
javax.servlet.ServletException: A MultiException has 1 exceptions. They are:
1. java.lang.NoSuchMethodException: Could not find a suitable constructor in com.example.AdminResource class.
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:535)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:483)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:420)
root cause
A MultiException has 1 exceptions. They are:
1. java.lang.NoSuchMethodException: Could not find a suitable constructor in com.example.AdminResource class.
org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:88)
org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:852)
org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:814)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:906)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:898)
org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:174)
org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:535)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:483)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:420)
root cause
java.lang.NoSuchMethodException: Could not find a suitable constructor in com.example.AdminResource class.
org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:189)
org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:159)
org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:850)
org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:814)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:906)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:898)
org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:174)
org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:535)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:483)
org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:420)
Technology Use:
Java - 8
Embedded tomcat - 8
Jersey 2.5.1 (REST)
JDBI 2.63.1 (DB Service)
Main.java
package com.example;
import com.core.db.DAOService;
import com.example.servlets.HelloWorldServlet;
import com.example.util.abc;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.skife.jdbi.v2.DBI;
public class Main {
public static void main (String[] args) {
Tomcat tomcat = new Tomcat();
tomcat.setPort(9000);
String baseDoc = new File("src/main/webapp").getAbsolutePath();
Context ctx = tomcat.addContext("/servletSample", baseDoc);
tomcat.addWebapp(null, "", baseDoc);
tomcat.addServlet(ctx, "HelloWorld", new HelloWorldServlet());
ctx.addServletMapping("/*", "HelloWorld");
try {
final DBI XYZDBI = new DBI(/*url*/, /*username*/, /*password*/);
/* DAOService is a class that has function getDetails() that retrive data from database */
final DAOService daoService = XYZDBI.onDemand(DAOService.class);
final org.glassfish.jersey.server.ResourceConfig jerseyResource = new org.glassfish.jersey.server.ResourceConfig();
/* Register XYZResource class instance with jersey Resouce */
jerseyResource.register(new XYZResource(daoService));
tomcat.start();
} catch (LifecycleException e) {
e.printStackTrace();
}
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>my-jersey</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>my-embed-tomcat</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Dependencies for Jersey Web Application -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>2.5.1</version>
</dependency>
<!-- Dependencies for Embedded Tomcat -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<!--org.skife.jdbi.v2-->
<groupId>org.jdbi</groupId>
<artifactId>jdbi</artifactId>
<version>2.63.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>microsoft.com</groupId>
<artifactId>sqlJdbc4</artifactId>
<version>4.0.2206.100</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
XYZResource.java
package com.example;
import com.core.db.DAOService;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;
#Path("resource")
public class XYZResource {
private final DAOService daoService;
public XYZResource(DAOService daoService) {
this.daoService = daoService;
}
#GET
public String helloworld() {
return "User - Resource";
}
#GET
#Path("/test")
public Response test() {
Set<String> User = daoService.getDetails();
Iterator<String> itr = User.iterator();
return Response.status(200).entity(itr.next().toString())
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET")
.build();
}
}
}
MyApplication.java
package com.example;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
public class MyApplication extends ResourceConfig {
public MyApplication() {
this.packages(MyApplication.class.getPackage().getName()).register(JspMvcFeature.class);
}
}
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" id="WebApp_ID" version="3.0">
<display-name>Jersey Jersey Application</display-name>
<filter>
<filter-name>Jersey Application Filter</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.example.MyApplication</param-value>
</init-param>
<!-- pass to next filter if Jersey/App returns 404 -->
<init-param>
<param-name>jersey.config.servlet.filter.forwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Jersey Application Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Update
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>jersey-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<finalName>my-embed-tomcat</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>8.0.28</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
s
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-guava</artifactId>
<version>2.25.1</version>
</dependency>
<!-- Dependencies for Jersey Web Application -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>2.5.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.5.1</version>
</dependency>
<!-- Dependencies for Embedded Tomcat -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<!--org.skife.jdbi.v2-->
<groupId>org.jdbi</groupId>
<artifactId>jdbi</artifactId>
<version>2.63.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0.rc2</version>
</dependency>
<dependency>
<groupId>microsoft.com</groupId>
<artifactId>sqlJdbc4</artifactId>
<version>4.0.2206.100</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
Main.java
package com.example;
import org.apache.catalina.Context;
import org.apache.catalina.servlets.DefaultServlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import javax.servlet.Filter;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.io.File;
/**
* #author Paul Samsotha.
*/
public class Main {
public static void main(String... args) throws Exception {
Tomcat tomcat = new Tomcat();
tomcat.setPort(9000);
File base = new File(".");
Context context = tomcat.addContext("", base.getAbsolutePath());
Tomcat.addServlet(context, "default", new DefaultServlet());
context.addServletMapping("/*", "default");
final FilterDef def = new FilterDef();
final FilterMap map = new FilterMap();
def.setFilterName("jerseyFilter");
def.setFilter(getJerseyFilter());
context.addFilterDef(def);
map.setFilterName("jerseyFilter");
map.addURLPattern("/api/*");
context.addFilterMap(map);
tomcat.start();
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static Filter getJerseyFilter() {
final ResourceConfig config = new ResourceConfig()
.register(new HelloResource(new Service()))
.register(JspMvcFeature.class)
.property(ServletProperties.FILTER_FORWARD_ON_404, true);
return new ServletContainer(config);
}
public static class Service {
public String getMessage() {
return "Hello Tomcat!";
}
}
#Path("tomcat")
public static class HelloResource {
private final Service service;
public HelloResource (Service service) {
this.service = service;
}
#GET
public String get() {
return this.service.getMessage();
}
}
}
You seem to have a lot of unnecessary stuff going on. You have two different configurations. Pick one. The problem is caused by the package scanning, which is picking up the resource and trying to register it.
Just get rid of the web.xml and MyApplication and just use the ResourceConfig you're currently using in the main class. Then you can register the servlet programmatically using Jersey's ServletContainer which is both a Filter and a HttpServlet. So you don't need the web.xml to configure Jersey as Filter. The ServletContainer is already a Filter
new SevletContainer(youResourceConfig);
I'm not too familiar with Tomcat embedded so I can't say how to register filters, but I'm sure a quick search will find you the answer. Just register the ServletContainer as a filter.
The jersey.config.servlet.filter.forwardOn404 init-param can also be configured with the filter registration. Or you can just configure it with the ResourceConfig, with its property method
resourceConfig.property("jersey.config.servlet.filter.forwardOn404", true);
Or better yet, use the constant
property(ServletProperties.FILTER_FORWARD_ON_404, true);
UPDATE
Here is a complete setup (based on your example) that works.
import org.apache.catalina.Context;
import org.apache.catalina.servlets.DefaultServlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import javax.servlet.Filter;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.io.File;
/**
* #author Paul Samsotha.
*/
public class Main {
public static void main(String... args) throws Exception {
Tomcat tomcat = new Tomcat();
tomcat.setPort(9000);
File base = new File(".");
Context context = tomcat.addContext("", base.getAbsolutePath());
Tomcat.addServlet(context, "default", new DefaultServlet());
context.addServletMapping("/*", "default");
final FilterDef def = new FilterDef();
final FilterMap map = new FilterMap();
def.setFilterName("jerseyFilter");
def.setFilter(getJerseyFilter());
context.addFilterDef(def);
map.setFilterName("jerseyFilter");
map.addURLPattern("/api/*");
context.addFilterMap(map);
tomcat.start();
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static Filter getJerseyFilter() {
final ResourceConfig config = new ResourceConfig()
.register(new HelloResource(new Service()))
.register(JspMvcFeature.class)
.property(ServletProperties.FILTER_FORWARD_ON_404, true);
return new ServletContainer(config);
}
public static class Service {
public String getMessage() {
return "Hello Tomcat!";
}
}
#Path("tomcat")
public static class HelloResource {
private final Service service;
public HelloResource (Service service) {
this.service = service;
}
#GET
public String get() {
return this.service.getMessage();
}
}
}
A few changes I made:
Not sure why you are using Tomcat 7 Servlet API. I Changed that to the 8.x version of your other Tomcat dependencies.
I also excluded the Servlet API from the Jersey MVC dependency, as that pulls in an older 2.4 Servlet API
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
I added a DefaultServlet to handle the pages. From testing (again I am not Tomcat expert), it seems this is required
With the example you should be able to GET
http://localhost:9000/api/tomcat
First, Thanks to peeskillet who solved my problem. I search on the net but I did not find the exact solution.
For better reference and understandability, I create a demo application on git-hub.
jersey-mvc-jsp-freemarker-with-embedded-tomcat
Application Used
Tomcat v8.x
Jersey v2.5.1
JSP
YML
Freemarker
HTML/Angular/JavaScript
final ResourceConfig config = new ResourceConfig()
.packages(Main.class.getPackage().getName())
.register(new Resource(new Core(), configuration)) // create instance of Resource and dynamically register
.register(JspMvcFeature.class) // register jspMVC
.register(FreemarkerMvcFeature.class) // register FreemarkerMVC
.property(ServletProperties.FILTER_FORWARD_ON_404, true);
return new ServletContainer(config);

Glassfish 3.0: Exception while deploying ejb module...Invalid ejb j ar: it contains zero ejb

I have a very simple ejb 3.0 module with maven, it has only two session beans one stateless and the other is singleton...when I am trying to deploy the project on Glassfish 3.0 server, I got this exception:
Error occurred during deployment: Exception while deploying the app :
Invalid ejb jar : it contains zero ejb. Note: 1. A valid ejb jar
requires at least one session, entity (1.x/2.x style), or
message-driven bean. 2. EJB3+ entity beans (#Entity) are POJOs and
please package them as library jar. 3. If the jar file contains valid
EJBs which are annotated with EJB component level annotations
(#Stateless, #Stateful, #MessageDriven, #Singleton), please check
server.log to see whether the annotations were processed properly..
Please see server.log for more details.
I verify the project with the Glassfish Verifier Tool, I got this exception, but I don't know what should I do?
Verifier output unparsable
Verifier output (C:\Users\Mariam.Moustafa\Documents\NetBeansProjects\web\EmployeesTimer\target\EmployeesTimer-1.0-SNAPSHOT.jar.xml) not found.
Here is The 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.unilever</groupId>
<artifactId>EmployeesTimer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>EmployeesTimer</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<!-- slf4j Logger -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
<repository>
<id>m2.dev.java.net</id>
<url>http://download.java.net/maven/2</url>
<layout>default</layout>
</repository>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
</project>
I confirm that I have 2 simple ejb
#Stateless public class EmployeesFacade {}
#Singleton public class TimerService {
#EJB EmployeesFacade emloyeesFacade;
}
Here is the code:
First #singleton ejb bean
package com.employeestimer.services;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.unilever.employeestimer.enums.ConnectionEnum;
import com.unilever.employeestimer.exceptions.BusinessException;
import java.io.IOException;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.naming.NamingException;
import javax.ws.rs.core.MediaType;
import org.codehaus.jackson.JsonProcessingException;
import org.slf4j.LoggerFactory;
#Singleton
public class TimerService {
static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TimerService.class);
#EJB
EmployeesFacade emloyeesFacade;
private final static String U_ENGAGE_URL = "http://..../webresources/employees/update";
#Schedule(second="*", minute="*/1",hour="*", persistent=false)
public void doWork() throws JsonProcessingException, IOException, NamingException{
try{
String input = emloyeesFacade.getUsersData(ConnectionEnum.USERNAME.getCode(), ConnectionEnum.PASSWORD.getCode());
consumeRESTfulWebService(input);
}catch(BusinessException e){
LOGGER.error("",e);
}
}
private void consumeRESTfulWebService(String input){
try {
LOGGER.debug("input = " + input);
Client client = Client.create();
WebResource webResource = client.resource(U_ENGAGE_URL);
ClientResponse response = webResource.type(MediaType.TEXT_PLAIN).post(ClientResponse.class, input);
if (response.getStatus() != ClientResponse.Status.NO_CONTENT.getStatusCode()) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
LOGGER.debug("Output from Server .... \n");
} catch (Exception e) {
LOGGER.error("",e);
}
}
}
and here is the Facade bean:
package com.employeestimer.services;
import com.employeestimer.beans.EmployeeData;
import com.employeestimer.enums.ConfigurationsEnum;
import com.employeestimer.enums.ConnectionEnum;
import com.employeestimer.exceptions.BusinessException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.PagedResultsResponseControl;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.slf4j.LoggerFactory;
#Stateless
public class EmployeesFacade {
static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(EmployeesFacade.class);
public String getLDAPConnectionType() {
return ConfigurationsEnum.LDAP_Connection_Type.getCode();
}
public String getDomain() {
return ConfigurationsEnum.DOMAIN.getCode();
}
public String getLDAPIP() {
return ConfigurationsEnum.LDAP_IP.getCode();
}
public String getLDAPPort() {
return ConfigurationsEnum.LDAP_PORT.getCode();
}
public String getLDAPBase() {
return ConfigurationsEnum.LDAP_BASE.getCode();
}
private LdapContext getContext(String username, String password) throws BusinessException, NamingException {
Hashtable env = new Hashtable();
if (getDomain() == null || getLDAPConnectionType() == null
|| getLDAPIP() == null || getLDAPPort() == null) {
throw new BusinessException("error_general");
}
String domain = getDomain();
String url = getLDAPConnectionType() + "://" + getLDAPIP() + ":" + getLDAPPort();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.REFERRAL, "follow");
if (!username.contains("#")) {
username = username + "#" + domain;
}
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
return new InitialLdapContext(env, null);
}
public String getUsersData(String username, String password) throws BusinessException, JsonProcessingException, IOException, NamingException {
List<EmployeeData> employeelist = new ArrayList<EmployeeData>();
EmployeeData employee;
String json = "";
try {
LdapContext context = getContext(username, password);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"mail"}; //, "sn", "cn", "c", "l"};
constraints.setReturningAttributes(attrIDs);
// Activate paged results
int pageSize = Integer.parseInt(ConnectionEnum.PAGESIZE.getCode());
byte[] cookie = null;
context.setRequestControls(new Control[]{new PagedResultsControl(pageSize,
Control.NONCRITICAL)});
int total;
NamingEnumeration<SearchResult> result = null;
do {
/* perform the search */
result = context.search(getLDAPBase(), "(&(objectClass=User)(c=EG))", constraints);
/* for each entry print out name + all attrs and values */
while (result != null && result.hasMore()) {
SearchResult entry = (SearchResult) result.next();
employee = new EmployeeData();
Attribute attribute;
if (entry.getAttributes() != null) {
if (entry.getAttributes().get("mail") != null) {
attribute = entry.getAttributes().get("mail");
employee.setEmail(attribute != null ? attribute.toString().replace("mail: ", "") : "");
employeelist.add(employee);
}
}
}
// Examine the paged results control response
Control[] controls = context.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
total = prrc.getResultSize();
if (total != 0) {
LOGGER.debug("***************** END-OF-PAGE " + "(total : " + total + ") *****************\n");
} else {
LOGGER.debug("***************** END-OF-PAGE " + "(Page no. " + i + ") ***************\n");
}
cookie = prrc.getCookie();
}
}
} else {
LOGGER.debug("No controls were sent from the server");
}
// Re-activate paged results
context.setRequestControls(new Control[]{new PagedResultsControl(
pageSize, cookie, Control.CRITICAL)});
} while (cookie != null);
//result.close();
context.close();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
json = ow.writeValueAsString(employeelist);
LOGGER.debug("\n \n \n ************* \n ************* \n ************* \n \n THE SIZE = " + employeelist.size() + "\n \n ************* \n ************* \n ************* \n \n \n");
} catch (NamingException ex) {
String detailmessage = ex.getExplanation();
if (detailmessage.contains(" 32 ")) {
// User not Found
throw new BusinessException("error_invalidUser");
} else if (detailmessage.contains(" 49 ")) {
if (detailmessage.contains(" 775 ") || detailmessage.contains(" 701 ") || detailmessage.contains(" 533 ")) {
//Invalid Account
throw new BusinessException("error_invalidAccount");
} else {
//Invalid Username or password
throw new BusinessException("error_invalidUsernamePassword");
}
} else {
LOGGER.error("error_general", ex);
throw new BusinessException("error_general");
}
}
return json;
}
}
I don't see a problem with your setup. If you really have some class annotated with #Singleton or #Stateless inside this project, this should work.
But one important point is that if you have any dependencies which are not available in the libs of Glassfish, you have to package them with your application which is not possible with EJB/JAR packaging. You have to package it as a WAR or EAR file.
For a quick start, try the following:
Change
<packaging>ejb</packaging>
to
<packaging>war</packaging>
and change
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
to
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
If you want to package it as an EAR, you can use the maven-ear-plugin.
See also:
Packaging EJB in JavaEE 6 WAR vs EAR
Maven 2 & Packaging ejb vs jar
Maven EJB packaging with dependent libraries
Adam Bien - Is Java EE 6 War The New EAR?

Spark-Cassandra Maven project with java source making scala-lib calls

I am fairly new to both Spark and Cassandra and need some guidance. I am setting up a maven project that uses Spark v1.3.1 and Cassandra v2.0.14. I am attempting the following:
1) Make a connection with Oracle DB using the following approach for data input; leveraging DataFrames new to Spark 1.3.0: http://www.sparkexpert.com/2015/03/28/loading-database-data-into-spark-using-data-sources-api/
2) Using spark-cassandra-connector to make the connection between the latter; found on github.
3) Once i have the DB data in DataFrame i should be able to convert to JavaRDD type and push to Cassandra keyspace as illustrated here: http://www.datastax.com/dev/blog/accessing-cassandra-from-spark-in-java
4) In short: [Oracle DB]<---[Spark]---[spark-cassandra-connector]--->[Cassandra]
The problem i'm having comes during (step 1 from above) the Scala-lib call in my Java code; more specifically during load function call: DataFrame jdbcDF = sqlContext.load(“jdbc”, options);
Runtime Error:
java.lang.ClassNotFoundException: scala.collection.GenTraversableOnce$class”
The above errors comes despite having tried several different version of the recommended 2.10.X Scala in my pom.xml file. From my prior research i'm thinking it might be a Spark-Scala compatibility issue. I've also read that i need to include the scala-lib.jar in my classpath but i am not certain how to do this with maven. Any ideas on any of this? I've included the pom.xml and java code below:
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dev</groupId>
<artifactId>spark-cassandra</artifactId>
<version>0.0.1-SPARK-CASSANDRA</version>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector_2.10</artifactId>
<version>1.0.0-rc4</version>
</dependency>
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
<version>1.0.0-rc4</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.dev.cassandra</groupId>
<artifactId>spark-cassandra</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.10.3</version>
</dependency>
<!--
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.10.0-M1</version>
</dependency>
-->
<!--
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-swing</artifactId>
<version>2.10.0-M1</version>
</dependency>
-->
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<mainClass>com.dev.cassandra.Main</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Plugin to create a single jar that includes all dependencies
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.dev.cassandra.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
JAVA CODE:
package com.dev.cassandra;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.*;
import org.apache.spark.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.*;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import oracle.jdbc.*;
import com.datastax.spark.connector.cql.CassandraConnector;
import static com.datastax.spark.connector.CassandraJavaUtil.*;
public class Main implements Serializable {
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(Main.class);
private static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String JDBC_USERNAME = "XXXXXO01";
private static final String JDBC_PWD = "XXXXXO01";
private static final String JDBC_CONNECTION_URL =
"jdbc:oracle:thin:" + JDBC_USERNAME + "/" + JDBC_PWD + "#CONNECTION VALUES";
private transient SparkConf conf;
private Main(SparkConf conf) {
this.conf = conf;
}
private void run() {
JavaSparkContext sc = new JavaSparkContext(conf);
SQLContext sqlContext = new SQLContext(sc);
generateData(sc);
compute(sc);
showResults(sc);
sc.stop();
}
private void generateData(JavaSparkContext sc) {
SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
System.out.println("AFTER SQL CONTEXT");
//Data source options
Map<String, String> options = new HashMap<>();
options.put("driver", JDBC_DRIVER);
options.put("url", JDBC_CONNECTION_URL);
options.put("dbtable","(SELECT * FROM XXX_SAMPLE_TABLE WHERE ROWNUM <=5)");
CassandraConnector connector = CassandraConnector.apply(sc.getConf());
try{
Class.forName(JDBC_DRIVER);
System.out.println("BEFORE jdbcDF");
//Load JDBC query result as DataFrame
DataFrame jdbcDF = sqlContext.load("jdbc", options);
System.out.println("AFTER jdbcDF");
List<Row> tableRows = jdbcDF.collectAsList();
System.out.println("AFTER tableRows");
for (Row tableRow : tableRows) {
System.out.println();
LOGGER.info(tableRow);
System.out.println();
}
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}
private void compute(JavaSparkContext sc) {
}
private void showResults(JavaSparkContext sc) {
}
public static void main(String[] args) throws InterruptedException
{
if (args.length != 2) {
System.err.println("Syntax: com.datastax.spark.dev.cassandra <Spark Master URL> <Cassandra contact point>");
System.exit(1);
}
//JavaSparkContext sc = new JavaSparkContext(new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]"));
SparkConf conf = new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]");
//SparkConf conf = new SparkConf();
//conf.setAppName("SparkJdbcDs");
//conf.setMaster(args[0]);
//conf.set("spark.cassandra.connection.host", args[1]);
Main app = new Main(conf);
app.run();
}
}
Thanks in advance!
The pom.xml is requesting the Scala 2.11 versions of some of the Spark JARs
<artifactId>spark-core_2.11</artifactId>
<artifactId>spark-sql_2.11</artifactId>
and the Scala 2.10 versions of another Spark JAR and the Cassandra connector JARs.
<artifactId>spark-streaming_2.10</artifactId>
<artifactId>spark-cassandra-connector_2.10</artifactId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
(Based on the naming convention for Scala Artifact IDs, which end with the version of Scala you want them built for.)
These need to be consistent (a) with each other and (b) with the Scala version you're actually using.

Categories