使用Spring启动配置Jersey

时间:2017-10-30 18:23:24

标签: java rest web-services spring-boot jersey

我正在尝试使用平针织物配置弹簧靴,但似乎平针织注释不适用于弹簧靴。 你可以帮帮我吗。

我在服务类中尝试过@RestController而不是@Component和@RequestMapping而不是@Path。

的pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.hotel</groupId>
    <artifactId>reservations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>reservations</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/>
        <!--  lookup parent from repository  -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Spring Boot Application Xml

package org.hotel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ReservationApplication {

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

}

带有泽西注释的服务类

package org.hotel.webservices;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Component
@Path("/rooms")
public class AddRoomService {

    @GET
    public String addRoomService(){
        return "success";
    }
}

2 个答案:

答案 0 :(得分:2)

在线关于此的好教程: Spring Boot Jersey Example July 14, 2017 by Lokesh Gupta。这似乎是你错过的部分。

泽西岛配置

1:现在我们有一个JAX-RS资源,我们希望从包含Jersey依赖项的spring boot应用程序访问它。让我们将此资源注册为泽西岛资源。

package com.howtodoinjava.jerseydemo;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
public class JerseyConfig extends ResourceConfig 
{
    public JerseyConfig() 
    {
        register(UserResource.class);
    }
}

查看@Component注释。它允许在spring boot自动扫描源文件夹中的java类时注册此类。

2:ResourceConfig提供了简化JAX-RS组件注册的高级功能。 3:使用SpringBootServletInitializer扩展spring启动应用程序。

package com.howtodoinjava.jerseydemo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class JerseydemoApplication extends SpringBootServletInitializer 
{
    public static void main(String[] args) 
    {
        new JerseydemoApplication().configure(new SpringApplicationBuilder    (JerseydemoApplication.class)).run(args);
    }
}

答案 1 :(得分:0)

只是为了补充来自尼古拉斯的答案,它同意提供出色的教程,但是如果您希望重组项目(例如为您的资源,模型等创建多个包),则必须确保JerseyConfig类和JerseydemoApplication类在相同的软件包,否则注册将无法进行。本教程中未明确提及。

我在使用IntelliJ Ultimate Edition并遵循上面尼古拉斯提供的相同教程时发现了这一点