【问题标题】:Why I cannot create endpoint with RestEasy为什么我不能用 RestEasy 创建端点
【发布时间】:2015-09-21 19:02:06
【问题描述】:

我正在尝试为我的 REST 端点实施测试,如下所述:http://antoniogoncalves.org/2012/12/19/test-your-jax-rs-2-0-web-service-uris-without-mocks/。提到的解决方案使用Jersey 实现JAX-RS,但我想使用RestEasy。当我运行测试时,我得到了

java.lang.UnsupportedOperationException
at org.jboss.resteasy.spi.ResteasyProviderFactory.createEndpoint(ResteasyProviderFactory.java:2176)

知道为什么JBossJAX-RS 实现不支持创建端点,但Jersey 支持(因为它在我帖子开头的链接下)?

【问题讨论】:

    标签: 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>
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      相关资源
      最近更新 更多