user1316830
user1316830

Reputation: 71

Apache CXF - JAXRSServerFactoryBean server started but not able to access the endpoint

I've created 2 classes, one RestService class and other is main class.

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.transport.http.HttpDestinationFactory;
import org.apache.cxf.transport.servlet.ServletDestinationFactory;

public class RestDemo {

public static void main(String[] args) {

    final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress("http://0.0.0.0:8080/");
    sf.setServiceClass(RestService.class);

    sf.setResourceProvider(RestService.class, new SingletonResourceProvider(new RestService()));

    ServletDestinationFactory destinationFactory = new ServletDestinationFactory();
    sf.getBus().setExtension(destinationFactory, HttpDestinationFactory.class);

    Server server = sf.create();
    server.start();
    System.out.println(server.isStarted());
      System.out.println(server.getDestination().getAddress().getAddress().getValue());
    System.out.println(server.getEndpoint().getEndpointInfo());

    try {
        Thread.sleep(1000000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

The sop output says the server is started but I'm not able to perform any curd operation on the endpoint. This is in linux. I also tried linux command "netstat -anp | grep 8080" to check whether port 8080 is listening but the output of this command is empty. Any help will be highly appreciable.

I need it as a standalone program rather than making it as a war with spring and deploying it in the webserver. I'm using the latest version of cxf which is having jetty within it so the start function is expected to started the server with embedded jetty but thats not happening.

Upvotes: 0

Views: 1251

Answers (1)

suenda
suenda

Reputation: 693

It seems you do not have all the required jars in your classpath. Here is a complete working project with a resource deployed :

https://drive.google.com/open?id=1KB8trntYF6N0MK8MDeOz_leowQzrgkJR

Run the following to run the main directly

mvn package exec:java -Dexec.mainClass=com.example.CXFStandalone

Main:

package com.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;


public class CXFStandalone {

    @Path("/")
    public static class RestService {

        @GET
        public String hello() {
            return "Hello world";
        }
    }

    public static void main(String[] args) {
        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setResourceClasses(RestService.class);
        sf.setResourceProvider(RestService.class, new SingletonResourceProvider(new RestService()));
        sf.setAddress("http://localhost:9000/");
        sf.create();
    }
}

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.example</groupId>
    <artifactId>cxf-standalone</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cxf-standalone</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.2.1</version>
        </dependency>
    </dependencies>
</project>

Upvotes: 1

Related Questions