您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JAX-RS不适用于Spring Boot 1.4.1

JAX-RS不适用于Spring Boot 1.4.1

看来最新版本的Spring Boot中没有这个问题。但是,当您要使用Spring Boot和Jersey创建应用程序时,此答案的内容仍可以用作指导。

可执行jar布局在Spring Boot 1.4.1中已更改:现在将应用程序的依赖项打包在BOOT-INF/lib而不是中lib,并且将应用程序自己的类打包在BOOT- INF/classesjar的根中。它影响泽西岛:

可执行jar布局的更改意味着,Jersey的类路径扫描中的限制现在会影响可执行jar文件以及可执行war文件。要变通解决此问题,您希望Jersey扫描的类应打包在一个jar中,并作为一个依赖项包含在中BOOT- INF/lib。然后,应将Spring Boot启动器配置为在启动时解压缩这些jar,以便Jersey可以扫描其内容

我发现注册类而不是包是可行的。请参阅以下步骤,使用Spring Boot和Jersey创建应用程序。

确保您的pom.xml文件声明spring-boot-starter-parent为父项目:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>

您还需要以下依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

还有Spring Boot Maven插件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

出于示例目的,创建带有批注的Jersey资源类,@Path并定义用于处理GET请求的资源方法,产生text/plain

@Path("/greetings")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response getGreeting() {
        return Response.ok("Hello, World!").build();
    }
}

然后创建一个扩展ResourceConfigApplication注册Jersey资源的类,并用对其进行注释@ApplicationPath。使用Spring Boot 1.4.1可以注册类而不是注册软件包:

@Component
@ApplicationPath("api")
public class JerseyConfig extends ResourceConfig {

    @postconstruct
    private void init() {
        registerClasses(GreetingResource.class);
    }
}

最后创建一个Spring Boot类来执行应用程序:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如果要测试此Web服务,可以使用JAX-RS Client API

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingResourceTest {

    @LocalServerPort
    private int port;

    private URI uri;

    @Before
    public void setUp() throws Exception {
        this.uri = new URI("http://localhost:" + port);
    }

    @Test
    public void testGreeting() {

        Client client = ClientBuilder.newClient();
        Response response = client.target(uri).path("api").path("greetings")
                                  .request(MediaType.TEXT_PLAIN).get();

        String entity = response.readEntity(String.class);
        assertEquals("Hello, World!", entity);
    }
}

要编译和运行该应用程序,请按照下列步骤操作:

请查看Spring Boot文档。有专门针对泽西岛部分

产生JSON时,请确保您已注册JSON提供程序ResourceConfig不过应该注意这一点(只需确保依赖项在类路径上)。

Java 2022/1/1 18:15:36 有403人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶