简体   繁体   中英

How to process HTTP request with multiple parameters in REST web service + limit the number range

My aim is to process

 http://localhost:8080/getuser?name=Mike&numberOfWorks=2&salary=1

Number of jobs is in range of 0-2, salary - from 0 (none) to 2 (huge). An additional requirement is to set a default value for age and salary. I have written this code in Java, however, it doesn't work properly (returns name and two def values) and I have no chance to debug it.

public Response getPerson( 
            @QueryParam("name") String name, 
            @DefaultValue("0") @QueryParam("{numberOfWorks: [0-2]}") int availability, 
            @DefaultValue("0") @QueryParam("{salary: [0-2]") int sort) throws NotFoundException{
        return Response.status(200).entity(name + ", " + availability + ", " + sort).build();

The return of this method in use of mentioned HTTP is

Mike, 0, 0

Edit

Unfortunately, approach with @Pattern isn't available for me. So I would be grateful for another options.

The annotation must mention the name of the query param:

@QueryParam("numberOfWorks")

If you use JAX-RS 2.0, you can add

@Pattern("[0-2]")

making it

@QueryParam("numberOfWorks") @Pattern("[0-2]") int availability

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