Why does jersey jackson/json processing fail on polymorphic types? - jackson

I have a simple example program that duplicates what I believe is a bug, but in case there are real experts out there who know better than I do what's going on, I'll post my issue here.
Here is my Main class:
public class Main {
public static final String BASE_URI = "http://0.0.0.0:8080/myapp/";
public static HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig()
.packages("com.example")
.register(EntityFilteringFeature.class)
.register(JacksonFeature.class)
;
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.shutdown();
}
}
And here's my Jersey resource:
#Path("myresource")
#Produces(MediaType.APPLICATION_JSON)
public class MyResource {
public static class InnerDataBase {
}
public static class InnerData extends InnerDataBase {
public String item1 = "item1";
public String item2 = "item2";
}
public static class Data {
public String name = "Got it!";
public InnerDataBase innerData = new InnerData();
}
#GET
public Data getIt() {
return new Data();
}
}
The item of note here is that I'm marshalling a class that contains a field whose concrete instance is a derived class with two fields ("item1" and item2") but whose type is actually that of the base class which has no fields. When I run this server and hit the endpoint, I get this:
{"name":"Got it!","innerData":{}}
If I comment out registration of EITHER the EntityFilteringFeature OR the JacksonFeature, the output becomes (as it should):
{"name":"Got it!","innerData":{"item1":"item1","item2":"item2"}}
Conclusion: It appears that the the Jackson media feature is not quite ready for prime time yet in Jersey 2.26-b09.
Additional Thoughts: When I comment out the JacksonFeature, I presume entity filtering is then done with the default Moxy provider. When I comment out EntityFilteringFeature, I presume then that jackson just takes over and handles marshalling for Jersey automatically.
Here's the dependencies section of my pom:
<properties>
<jersey.version>2.26-b09</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
</dependency>
<!-- both json modules may be included as they're enabled in the ResourceConfig -->
<dependency> <!-- use this one when using moxy json processing -->
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency> <!-- use this one when using jackson json processing -->
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
Your thoughts? Am I missing something important here?

Related

junit5 with selenium testcontainer not working

Goal - I want to be able to write selenium tests, using Junit 5, and testcontainers. I am writing a very simple code to just be able to check an attribute of the search bar of google's homepage.
Issue - chrome.getWebDriver(); returns null. Am I missing something?
exception - java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.manage()" because "driver" is null.
this is caused as I try to set an implicit wait after initializing WebDriver.
my pom.xml -
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<test-containers.version>1.16.0</test-containers.version>
<selenium.version>3.141.59</selenium.version>
<junit.version>5.8.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>selenium</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
My test class -
#TestInstance(Lifecycle.PER_CLASS)
#Testcontainers
public class GoogleTest {
#Container
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withCapabilities(Config.getChromeOptions());
private GooglePage googlePage;
#BeforeAll
public void setup() throws Exception {
// ISSUE IS WITH THE LINE BELOW
WebDriver driver = chrome.getWebDriver();
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
// trying to use page object model, just navigates to the google homepage
googlePage = new GooglePage(driver);
}
#Test
public void testTitle() throws Exception {
WebElement element = googlePage.getSearchBar();
Thread.sleep(5000);
assertEquals(element.getAttribute("role"), "combobox");
}
}
In case you need it, the google drive link for the code
got my mistake, if using junit5, fields should be private static final I think...
I was still using public...

Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration

I'm trying to connect an API witha postgres database. And for that, I'm using Spring Boot.
When testing the api, i keep getting the error
Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration.
I understand that i need a #Bean for ServletController, but I donw know exactly what this bean should do. When i made a generic one, but then the console told me to revisit it. I have no idea what to do here, and stackoverflow hasn't helped either.
Here's my Application file:
#SpringBootApplication
#SwaggerDefinition
#EnableSwagger2
#EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX#XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
(the annotations are there, but for some reason, the stackoverflow did not accept them) and my pom is
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</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>
</plugin>
</plugins>
</build>
As you have excluded Tomcat from web and added Tomcat provided scope so I assume you are trying to create war and deploy on external Tomcat, so you have to extend SpringBootServletInitializer and override configure method.
#SpringBootApplication
#SwaggerDefinition
#EnableSwagger2
#EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX#XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
Or if you are running with embedded Tomcat then don't change anything in main class and remove exclusion from web and remove Tomcat jar:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
and remove Tomcat jar below:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

Can't get json from Swagger (swagger-jaxrs)

Actually i am trying to generate swagger.json for my REST services.
i succeeded to do it using jersy, but now we use don't use jersy in our services, we just use Jax-rs, so now i am not able to generate it and got 404 error code.
Here are my pom dependencies
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
and this is my app config application
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.skios.endpoint.EmployeeEndpoint;
import com.skios.endpoint.ShopsEndpoint;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
#ApplicationPath("api")
public class AppConfiguration extends Application {
public AppConfiguration() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[] { "http" });
beanConfig.setHost("localhost:7070");
beanConfig.setBasePath("app");
beanConfig.setResourcePackage("com.skios.endpoint");
beanConfig.setScan(true);
}
#SuppressWarnings({ "rawtypes", "unchecked" })
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
resources.add(ShopsEndpoint.class);
resources.add(EmployeeEndpoint.class);
// ...
resources.add(ApiListingResource.class);
resources.add(SwaggerSerializers.class);
return resources;
}
}
and these are my end points
#Api(value = "/employees", description = "Endpoint for employee listing")
#Path("/employees")
public class EmployeeEndpoint {}
#Path("/shops")
#Api(value="/shops", description="Shops")
public class ShopsEndpoint {}
andnow when i am trying to enter the path of swagger.json file
http://localhost:7070/MavenSwaggerTest/api/swagger.json
i got 404 error code.
so what is the problem? can any one help me?
thanks
You haven't configure your ApplicationPath and BasePathcorrectly.
Configure it like this.
#ApplicationPath("/api")
beanConfig.setBasePath("/api");
Now http://localhost:7070/MavenSwaggerTest/api/swagger.json would work, unless there is any other issue.

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!

Apache Camel failed to create endpoint

I'm new on Apache Camel and I need to integrate it with Apache ActiveMQ.
I tried a basic example, I installed on my computer FileZilla Server and ActiveMQ (works both) and I want to copy a file from the local server to the JMS queue that I created in Active MQ; the problem is that the method start() of CamelContext throws org.apache.camel.FailedToCreateRouteException
Here is my code (the address in ftpLocation is the static address of my computer):
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
public class FtpToJmsExample
{
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String ftpLocation = "ftp://192.168.1.10/incoming?username=Luca&password=Luca";
public void start() throws Exception
{
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
context.addRoutes(
new RouteBuilder() {
public void configure()
{
from(ftpLocation).
process(executeFirstProcessor()).
to("jms:TESTQUEUE");
}
});
System.out.println("START");
context.start();
System.out.println("wait");
System.out.println(loaded);
Thread.sleep(3000);
while (loaded == false)
{
System.out.println("in attesa\n");
}
context.stop();
System.out.println("stop context!");
System.out.println(loaded);
}
public static void main(String args[]) throws Exception
{
FtpToJmsExample example = new FtpToJmsExample();
example.start();
}
private Processor executeFirstProcessor()
{
return new Processor() {
#Override
public void process(Exchange exchange)
{
System.out.println("We just downloaded : "+
exchange.getIn().getHeader("CamelFileName"));
loaded = true;
}
};
}
}
This is the 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel</groupId>
<artifactId>examples</artifactId>
<version>2.11.0</version>
</parent>
<artifactId>camel-example-jms-file</artifactId>
<name>Camel :: Example :: JMS-File</name>
<description>An example that persists messages from FTP site to JMS</description>
<dependencies>
<!-- Camel dependencies -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
</dependency>
<!-- ActiveMQ dependencies -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Example</id>
<properties>
<target.main.class>com.ftpToJms.FtpToJMSExample</target.main.class>
</properties>
</profile>
</profiles>
</project>
And this is the report of the error
Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[ftp://192.168.1.10/incoming?username=Luc... because of Failed to resolve endpoint: ftp://192.168.1.10/incoming?password=Luca&username=Luca due to: No component found with scheme: ftp
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:181)
at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:750)
at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1829)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1609)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1478)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1446)
at ftptojms.FtpToJmsExample.start(FtpToJmsExample.java:51)
at ftptojms.FtpToJmsExample.main(FtpToJmsExample.java:73)
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: ftp://192.168.1.10/incoming?password=Luca&username=Luca due to: No component found with scheme: ftp
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:514)
at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:62)
at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:191)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:108)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:114)
at org.apache.camel.model.FromDefinition.resolveEndpoint(FromDefinition.java:72)
at org.apache.camel.impl.DefaultRouteContext.getEndpoint(DefaultRouteContext.java:90)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:861)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:176)
... 8 more
Someone can help me?
Sorry for the long post and the not-perfect english.
You need to add camel-ftp to your classpath. If you use Maven then its easy as just add it as dependency to the pom.xml