【问题标题】:Spring Boot 2 Jersey Implementation not workingSpring Boot 2 Jersey 实现不起作用
【发布时间】:2018-07-25 17:04:52
【问题描述】:

我正在使用 Spring Boot 2 来使用 Jersey 实现 REST 实现。我无法将 Resource 类注册为 Jersey 实现的一部分。我收到编译器错误。

错误:

编译器错误:JerseyConfig 类型的层次结构不一致。

服务/资源代码:

@Service
@Path("/api/v1")
public class PersonResource {

    private final PersonRepository personRepo;

    @Autowired
    public PersonResource(PersonRepository personRepo) {
        this.personRepo = personRepo;
    }


    @GET
    @Path( "/persons")
    @Produces("application/json")

    public List<Person> getAllPerson(){
        List<Person> persons = personRepo.findAll();
        return persons;

    }

    @GET
    @Path( "/persons/{id}")
    @Produces("application/json")
    public Response findPersonById(@PathParam("id") String id) throws NumberFormatException, Exception{
        Person person = personRepo.findById(Long.valueOf(id)).orElseThrow( () -> new Exception("Unable to find a person with id: " + id));
        return Response.ok(person, MediaType.APPLICATION_JSON).build();

}

配置:

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.example.demo.rest.PersonResource;

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {

        register(PersonResource.class);
    }
}

Maven 依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

我不确定我在配置方面缺少什么。任何有助于解决此问题的指针表示赞赏。

GitHub 代码链接:

https://github.com/sureshpec04/jersey-rest-demo

【问题讨论】:

  • 您在 findPersonById 上缺少右括号。我没有看到任何其他编译错误。此外,您应该在资源类上使用@Component 而不是@Service(尽管这不是编译错误)。同时删除 javax.ws.rs-api 依赖项。这不是必需的。已经被 Jersey 拉进来了(你用错了版本)
  • @Paul - 如果我删除 javax.ws.rs-api 依赖项 GET、Path、Response 没有得到解决。是否应该通过 jersey starters 依赖来解决?
  • 那你的环境有问题。 Jersey 应该把它拉进去。确保你有 Spring boot 父 pom 集。尝试清理您的工作区和本地 Maven 存储库。并查看上面的链接。
  • @Paul - 我将从头开始创建一个新项目并更新您。

标签: spring-boot jax-rs jersey-2.0


【解决方案1】:

只需将 JerseyConfig 类名重命名为 AppConfig 或 SomethingConfig。

【讨论】:

  • 还是没有运气。用 github 链接更新了我的问题。
  • 我没有收到错误 RestConfig1。罐子可能已损坏。尝试清理您的 Maven 存储库并运行您的项目
  • 清理我的 Maven 存储库解决了这个问题。谢谢!
【解决方案2】:

JerseyConfig 重命名为 YourCustomNameConfig 并转到 pom.xml 单击右键 maven->reimport 或重新构建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2021-10-17
    • 2021-01-02
    • 1970-01-01
    • 2016-02-21
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多