问题似乎是一些糟糕的错误消息。
我在 eclipse 中创建了一个动态 Web 应用程序项目,运行时是 TomEE+ 1.7.2
这就是发布商
package study;
import java.io.IOException;
import java.net.InetSocketAddress;
import javax.ws.rs.ext.RuntimeDelegate;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class StandaloneJaxRsServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(
"localhost", 8765), 8);
HttpHandler requestHandler = RuntimeDelegate.getInstance()
.createEndpoint(new JaxRSApplication(), HttpHandler.class); //<<<<< line 15
server.createContext("/jaxrs/", requestHandler);
server.start();
}
}
这是应用程序
package study;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath(value="/jaxrs")
public class JaxRSApplication extends Application{
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(NotSingletonResource.class);
return set ;
}
//
// @Override
// public Set<Object> getSingletons() {
// Set<Object> set = new HashSet<>();
// set.add(new SingletonResource());
// return set ;
// }
}
这就是资源
package study;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/notSingletonResource")
public class NotSingletonResource {
private volatile int counter = 0;
@GET
@Produces({MediaType.TEXT_HTML})
public String getHello() {
return "Not singleton resource " + counter++;
}
}
当我尝试运行 StandaloneJaxRsServer.main() 时,我得到
Exception in thread "main" java.lang.IllegalArgumentException
at org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl.createEndpoint(RuntimeDelegateImpl.java:104)
at study.StandaloneJaxRsServer.main(StandaloneJaxRsServer.java:15)
但是,IllegalArgumentException 并不能说明什么。我做错了什么?
最佳答案
RuntimeDelegate
接口(interface)的 javadoc 声明“抛出:IllegalArgumentException - 如果应用程序为 null 或不支持请求的端点类型。”对于 JAX-RS 应用程序,我认为您应该使用 org.apache.cxf.jaxrs.JAXRSServerFactoryBean,并使用创建端点的结果来设置服务器。
关于java - 我无法使用 TomEE 和 Eclipse 发布简单的 JAX-RS 服务 (IllegalArgumentException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075612/