Apache CXF - JAXRSServerFactoryBean服务器已启动但无法访问端点

时间:2018-01-18 17:54:11

标签: java cxf

我创建了2个类,一个RestService类,另一个是主类。

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();
    }
}

}

sop输出表示服务器已启动,但我无法在端点上执行任何curd操作。这是在linux中。我也试过linux命令" netstat -anp | grep 8080"检查端口8080是否正在侦听但此命令的输出为空。任何帮助都会非常值得一提。

我需要它作为一个独立的程序,而不是将它作为一个与spring的战争并在Web服务器中部署它。我使用的是最新版本的cxf,其中包含jetty,因此启动功能预计将启动带有嵌入式码头的服务器但不会发生这种情况。

1 个答案:

答案 0 :(得分:1)

您的类路径中似乎没有所有必需的jar。这是一个部署了资源的完整工作项目:

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

运行以下命令直接运行main

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

主:

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>