Spring Boot 2 Jersey实现不起作用



我使用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

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

JerseyConfig重命名为YourCustomNameConfig,然后转到pom.xml,右键单击maven->重新导入或重建。

最新更新