简体   繁体   中英

multiple parameters in web service java JAX-RS

I am writing Jersey RESTful web services. All my method like add, delete, get work. But i want create method who showing what book what user borrowing.

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;
}  }

And now i want using method with multiple parameters

@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
} }

And i got error, but all looks fine:

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.

Any tip? :)

Resource methods may not have more than one entity parameter. You can have multiple @PathParam , @QueryParam , etc. but only one unannotated parameter in each resource method.

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

You could remove UserMaker user from your resource method and instead pass the user id to userManagementWS.hireBook(idU, book) . And then retrieve the user from your Map<Long, UserMaker> via userMaker.get(idU) . https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-

But I'd recommend you restructure your api. I found this link pretty informative http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM