gpt4 book ai didi

java - Web 服务 java JAX-RS 中的多个参数

转载 作者:行者123 更新时间:2023-12-02 02:30:59 27 4
gpt4 key购买 nike

我正在编写 Jersey RESTful Web 服务。我所有的方法,如添加、删除、开始工作。但我想要创建显示用户借阅什么书的方法。

public class UserManagement {

private Map<Long, UserMaker> userMaker = DataBase.getUserMaker();

public UserManagement(){ //id , name, surname, nin, status of book
userMaker.put((long) 1, new UserMaker(1,"John", "Castles", 12345,0));

public UserMaker hireBook(UserMaker user, BookMaker book){ // method who update status hiring book , if 0 that means book is rented
if(user.getId() <= 0){
return null;
}
book.setStatus((int) user.getId()); //
user.setWhatIhave((int) (book.getId())); // convert int to long
userMaker.put(user.getId(), user);
return user;
} }

现在我想使用具有多个参数的方法

@Path("/user")
public class UserCRUD {

UserManagement userManagementWS = new UserManagement();

@PUT
@Path("/{idU}/{idB}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserMaker hireBook(
@PathParam("idU") long idU, UserMaker user,
@PathParam("idB") long idB, BookMaker book) {
user.setId(idU);
return userManagementWS.hireBook(user, book); //borrowing books
} }

我遇到了错误,但一切看起来都很好:

Method public project.emil.lib.model.UserMaker project.emil.lib.resources.UserCRUD.hireBook(long,project.emil.lib.model.UserMaker,long,project.emil.lib.model.BookMaker) on resource class project.emil.lib.resources.UserCRUD contains multiple parameters with no annotation. Unable to resolve the injection source.

有什么建议吗? :)

最佳答案

资源方法不能有多个实体参数。您可以有多个@PathParam , @QueryParam等,但每个资源方法中只有一个未注释的参数。

3.3.2.1 Entity Parameters The value of a parameter not annotated with @FormParam or any of the annotations listed in in Section 3.2, called the entity parameter, is mapped from the request entity body. Conversion between an entity body and a Java type is the responsibility of an entity provider, see Section 4.2. Resource methods MUST have at most one entity parameter.

http://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf

您可以删除UserMaker user从您的资源方法中将用户 ID 传递给 userManagementWS.hireBook(idU, book) 。然后从您的Map<Long, UserMaker>中检索用户通过userMaker.get(idU)https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-

但我建议您重组您的 api。我发现这个链接内容非常丰富http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api .

关于java - Web 服务 java JAX-RS 中的多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47121182/

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