【发布时间】: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 代码链接:
【问题讨论】:
-
您在
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