使用 Spring Boot 配置 Jersey

shi*_*vam 1 java rest web-services jersey spring-boot

我正在尝试使用 jersey 配置 spring boot,但似乎 jersey 注释不适用于 spring boot。你能帮帮我吗。

我在服务类中尝试过@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>
Run Code Online (Sandbox Code Playgroud)

Spring Boot 应用程序 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

带有球衣注释的服务类

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

K.N*_*las 5

不错的在线教程: Lokesh Gupta 2017 年 7 月 14 日的 Spring Boot Jersey 示例。这似乎是您缺少的部分。

泽西配置

1:现在我们有一个 JAX-RS 资源,我们想从包含 Jersey 依赖项的 Spring Boot 应用程序访问它。让我们将此资源注册为 Jersey 资源。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

查看@Component 注释。它使这个类能够在 spring boot 自动扫描源文件夹中的 java 类时注册。

2:ResourceConfig提供高级功能来简化 JAX-RS 组件的注册。3:使用SpringBootServletInitializer.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)