gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:44:09 24 4
gpt4 key购买 nike

我创建了 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 的 war 并将其部署在网络服务器中。我正在使用最新版本的 cxf,其中包含 jetty,因此启动函数预计将启动带有嵌入式 jetty 的服务器,但事实并非如此。

最佳答案

您的类路径中似乎没有所有必需的 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>

关于java - Apache CXF - JAXRSServerFactoryBean 服务器已启动但无法访问端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com