JAX-RS does not work with Spring Boot 1.4.1

前端 未结 3 1363
时光取名叫无心
时光取名叫无心 2020-12-09 07:08

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.IllegalStateExc         


        
3条回答
  •  攒了一身酷
    2020-12-09 08:11

    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.


    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 classpath scanning limitations

    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.

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

    Creating a web application with Spring Boot and Jersey

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

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
    
    

    You also need the following dependencies:

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-jersey
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            test
        
    
    

    And the Spring Boot Maven plugin:

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    

    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();
        }
    }
    

    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);
        }
    }
    

    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);
        }
    }
    

    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:

    • 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.

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

    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).

提交回复
热议问题