java - Jersey ,Tomcat : The requested resource is not available error in InjelliJ

标签 java spring-boot jersey-2.0

我是 Jersey 的新手,正在尝试将一个项目从 Spring MVC 转换为 Jersey。但是,对于我当前的构建,所有请求都会返回一个 resource not available 错误。任何帮助将不胜感激。

我的依赖项:

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.4.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-jersey")
    runtime('org.hsqldb:hsqldb')
    compileOnly("org.springframework.boot:spring-boot-starter-tomcat")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

我的 Jersey 配置

    @Configuration
public class JersyConfig extends ResourceConfig {

    public JersyConfig() {
        registerEndpoints();
        configureSwagger();
    }

    private void configureSwagger() {
        register(ApiListingResource.class);
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8090");
        beanConfig.setBasePath("/");
        beanConfig.setResourcePackage(OwnerController.class.getPackage().getName());
        beanConfig.setPrettyPrint(true);
        beanConfig.setScan(true);
    }

    private void registerEndpoints() {
        register(OwnerController.class);
    }

}


    @Api(value = "Owner controller", tags = {"Owner resource"})
public class OwnerController {

    private final ClinicService clinicService;

    @Autowired
    public OwnerController(ClinicService clinicService) {
        this.clinicService = clinicService;
    }

    @GET
    @Path("/{ownerId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get owner by id", response = Owner.class)
    public Response getOwner(
            @ApiParam(name = "owner id", value = "owner id that must be fetched") @PathParam("ownerId") int id ) {
        Owner owner = clinicService.findOwnerById(id);
        return Response.status(200).entity(owner).build();
    }

    @GET
    @Path("/owners")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get all owners", response = Owner.class, responseContainer = "List")
    public Response getOwners() {
        List<Owner> owner = (List<Owner>) clinicService.findAllOwners();
        return Response.status(200).entity(owner).build();
    }

}

最佳答案

使用 JerseryConfig() 构造函数中的 packages() 方法注册包含 Jersey 资源的包 -

public JersyConfig() {
    packages("PACKAGE_CONTAINING_JERSEY_RESOURCES");
    registerEndpoints();
    configureSwagger();
}

关于java - Jersey ,Tomcat : The requested resource is not available error in InjelliJ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742209/

相关文章:

java - 如何在 Spring Boot 中创建调用包含构造函数注入(inject)的服务的测试?

java - Jetty Embedded, Jersey 2, Weld

java - 灰熊/ Jersey : Request injected into ContainerRequestFilter is null

java - ASyncTask 导致 getDatabaseLocked

java.lang.UnsupportedOperationException : Can't convert to color: type=0x1 异常

java - 如何使用 Spring Boot 和 Maven 设置配置文件?

java - Jersey @PreMatching 和 1 个提供商中的名称绑定(bind)

java - 地址已在使用 ["ajp-nio-1099"]

java - 无锁和无等待线程安全延迟初始化

java - Spring Boot CORS 与 HTTPS 失败