JAX-RS不适用于Spring Boot 1.4.1 [英] JAX-RS does not work with Spring Boot 1.4.1

查看:126
本文介绍了JAX-RS不适用于Spring Boot 1.4.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Boot 1.4.1.RELEASE开发一个简单的基于JAX-RS的Web服务。但是获得此异常 -

I am trying to develop a simple JAX-RS based web service using Spring Boot version 1.4.1.RELEASE. However getting this exception -

java.lang.IllegalStateException: No generator was provided and there is no default generator registered
at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:308) ~[hk2-api-2.5.0-b05.jar:na]
at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:268) ~[hk2-api-2.5.0-b05.jar:na]
at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:138) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:123) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:330) ~[jersey-server-2.23.2.jar:na]
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392) ~[jersey-container-servlet-core-2.23.2.jar:na]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177) ~[jersey-container-servlet-core-2.23.2.jar:na]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369) ~[jersey-container-servlet-core-2.23.2.jar:na]

这是我的程序详情 -

Here are my program details -

POM.xml中包含的依赖关系 -

Dependencies included in POM.xml -

<dependencies>
    <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>
</dependencies>

这里是JerseyConfig文件 -

And here is JerseyConfig file -

package com.test.main;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
import com.test.resources.TutorialResource;

@Component
public class JerseyConfig extends ResourceConfig{
    public JerseyConfig() {
        register(TutorialResource.class);
        packages("com.test.resources");
    }
}


推荐答案


重要说明:在最新版本的Spring Boot中看起来不存在此问题。但是,当您想要使用Spring Boot和Jersey创建应用程序时,此答案的内容仍可用作指南。

Important: Looks like this issue is not present in most recent versions of Spring Boot. However the content of this answer can still be used as a guide when you want to create an application with Spring Boot and Jersey.






JAR的布局在Spring Boot 1.4.1中发生了变化



可执行jar的布局已经改变了 Boot 1.4.1:应用程序的依赖项现在打包在 BOOT-INF / lib 而不是 lib ,以及应用程序自己的类中现在打包在 BOOT-INF / classes 中,而不是jar的根目录。它会影响泽西岛:


The layout of the JAR has changed in Spring Boot 1.4.1

The layout of executable jars has changed in Spring Boot 1.4.1: application’s dependencies are now packaged in BOOT-INF/lib rather than lib, and application’s own classes are now packaged in BOOT-INF/classes rather than the root of the jar. And it affects Jersey:


Jersey类路径扫描限制

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

The change to the layout of executable jars means that a limitation in Jersey’s classpath scanning now affects executable jar files as well as executable war files. To work around the problem, classes that you wish to be scanned by Jersey should be packaged in a jar and included as a dependency in BOOT-INF/lib. The Spring Boot launcher should then be configured to unpack those jars on start up so that Jersey can scan their contents.

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

I've found that registering classes instead of packages works. See below the steps to create an application with Spring Boot and Jersey.

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

Ensure your pom.xml file declares spring-boot-starter-parent as the parent project:

<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插件:

And the Spring Boot Maven plugin:

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

例如,创建一个使用 @Path 并定义资源处理 GET 请求的方法,生成 text / plain

For example purposes, create a Jersey resource class annotated with @Path and define a resource method to handle GET requests, producing text/plain:

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

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

然后创建一个扩展 ResourceConfig 申请 注册Jersey资源并使用 @ApplicationPath 。注册类而不是注册包与Spring Boot 1.4.1一起使用:

Then create a class that extends ResourceConfig or Application to register the Jersey resources and annotated it with @ApplicationPath. Registering classes instead of registering packages works with Spring Boot 1.4.1:

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

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

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

And finally create a Spring Boot class to execute the application:

@SpringBootApplication
public class Application {

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

如果你想测试这个网络服务,你可以使用 JAX-RS Client API

If you want to test this web service, you can use the 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);
    }
}

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

To compile and run the application, follow these steps:


  • 打开命令行窗口或终端。

  • 导航到项目的根目录, pom.xml 所在的位置。

  • 编译项​​目: mvn clean compile

  • 打包应用程序: mvn package

  • 查看目标目录。您应该看到一个具有以下或类似名称的文件: spring-jersey-1.0-SNAPSHOT.jar

  • 更改为目标目录。

  • 执行JAR: java -jar spring-jersey-1.0-SNAPSHOT.jar

  • 该应用程序应该在 http:// localhost:8080 / api / greetings

  • Open a command line window or terminal.
  • Navigate to the root directory of the project, where the pom.xml resides.
  • Compile the project: mvn clean compile.
  • Package the application: mvn package.
  • Look in the target directory. You should see a file with the following or a similar name: spring-jersey-1.0-SNAPSHOT.jar.
  • Change into the target directory.
  • Execute the JAR: java -jar spring-jersey-1.0-SNAPSHOT.jar.
  • The application should be available at http://localhost:8080/api/greetings.

注1:查看Spring Boot文档。有一个专门针对泽西岛的部分

Note 1: Have a look at the Spring Boot documentation. There's a section dedicated to Jersey.

注2:生成JSON时,请确保有 JSON提供商注册 ResourceConfig 应该注意这一点(只需确保依赖关系在类路径上)。

Note 2: When producing JSON, ensure you have a JSON provider registered. ResourceConfig should take care of that though (just ensure that the dependencies are on the classpath).

这篇关于JAX-RS不适用于Spring Boot 1.4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆