【问题标题】:Why I cannot create endpoint with RestEasy为什么我不能用 RestEasy 创建端点
【发布时间】:2015-09-21 19:02:06
【问题描述】:
【问题讨论】:
标签:
jboss
jersey
jax-rs
resteasy
【解决方案1】:
查看 RESTeasy 文档,Chapter 36. Embedded Containers。您将找到四种不同类型容器的示例及其在测试中的用法:
- 暗流
- Sun HttpServer
- TJWS
- 网络
你可以选择你的口味。
这是一个使用 Sun HttpServer 的完整示例(如您链接到的示例):
public class SunHttpServerTest {
@Path("simple")
public static class SimpleResource {
@GET
public String get() {
return "Hello Sun";
}
}
private HttpContextBuilder contextBuilder;
private HttpServer httpServer;
@Before
public void setUp() throws Exception {
httpServer = HttpServer.create(new InetSocketAddress(8000), 10);
contextBuilder = new HttpContextBuilder();
contextBuilder.getDeployment().getActualResourceClasses().add(SimpleResource.class);
HttpContext context = contextBuilder.bind(httpServer);
context.getAttributes().put("some.config.info", "42");
httpServer.start();
}
@After
public void tearDown() {
contextBuilder.cleanup();
httpServer.stop(0);
}
@Test
public void shouldReturnCorrectMessage() {
Client client = ClientBuilder.newClient();
Response response = client.target("http://localhost:8000/simple")
.request().get();
assertEquals(200, response.getStatus());
String message = response.readEntity(String.class);
assertEquals("Hello Sun", message);
System.out.println(message);
response.close();
}
}
这个工作还需要以下依赖项
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jdk-http</artifactId>
<version>3.0.9.Final</version>
</dependency>