gpt4 book ai didi

java - 使 MessageBodyWriter 不消耗资源类并返回资源一中的子资源类

转载 作者:行者123 更新时间:2023-12-02 02:49:55 24 4
gpt4 key购买 nike

我想创建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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com