Web 服务中的多个参数 java JAX-RS
multiple parameters in web service java JAX-RS
我正在编写 Jersey RESTful 网络服务。我所有的方法,如添加、删除、开始工作。但是我想要创建方法来显示用户借了什么书。
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)
。然后通过 userMaker.get(idU)
从您的 Map<Long, UserMaker>
检索用户。
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-
但我建议您重组 api。我发现这个 link 非常有用 http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api。
我正在编写 Jersey RESTful 网络服务。我所有的方法,如添加、删除、开始工作。但是我想要创建方法来显示用户借了什么书。
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)
。然后通过 userMaker.get(idU)
从您的 Map<Long, UserMaker>
检索用户。
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-
但我建议您重组 api。我发现这个 link 非常有用 http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api。