作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建rest api。我不想在每个资源类中重复使用绝对路径,所以我想使其分层。所以而不是像这样:
@Path("/")
@Component
@Scope("request")
public class UsersResource extends Application {
@GET @Path("users/{id}/roles")
public Role usersResource(@PathParam("id") Long id) {
}
}
我想返回依赖资源,如下所示:
@Component
@Scope("request")
public class UsersResource {
@Inject private UserRolesResource userRolesResource;
@GET @Path("{id}/roles") public UserRolesResource getRolesResource(@PathParam("id") Long id) {
return userRolesResource.withUserId(id);
}
}
@Component
@Scope("request")
public class UserRolesResource {
private Long userId;
@GET public String getAll() {
return userId + "=r1, r2, r3";
}
@GET @Path("{id}") public String getById(@PathParam("id") Long id) {
return "userId " + userId + "roleId=" + id;
}
public UserRolesResource withUserId(Long userId) {
this.userId = userId;
return this;
}
}
如您所见,我正在尝试返回 UserRolesResource
类,而不是数据。我现在想 jersey/jaxrs 不要序列化资源类,而是应该将控制移至返回的资源,并允许正确处理和序列化。
我将 Spring Boot 与 jaxrs 和 jersey 结合使用。
我不可能,因为我看到正在运行的解决方案,我可能只是缺少一些正文编写器配置。
提前致谢
最佳答案
哦,在我返回子资源的地方,我有 @GET
注释,在子资源条目上是非法的:@GET @Path("{id}/roles") public UserRolesResource getRolesResource(@PathParam("id") 长 id) {
删除 @GET
后,它起作用了。
关于java - 使 MessageBodyWriter 不消耗资源类并返回资源一中的子资源类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57113259/
我是一名优秀的程序员,十分优秀!