How do I retrieve query parameters in Spring Boot?

前端 未结 5 953
慢半拍i
慢半拍i 2020-12-12 14:23

I am developing a project using Spring Boot. I\'ve a controller which accepts GET requests.

Currently I\'m accepting requests to the following kind of URLs:

相关标签:
5条回答
  • 2020-12-12 15:03

    In Spring boot: 2.1.6, you can use like below:

        @GetMapping("/orders")
        @ApiOperation(value = "retrieve orders", response = OrderResponse.class, responseContainer = "List")
        public List<OrderResponse> getOrders(
                @RequestParam(value = "creationDateTimeFrom", required = true) String creationDateTimeFrom,
                @RequestParam(value = "creationDateTimeTo", required = true) String creationDateTimeTo,
                @RequestParam(value = "location_id", required = true) String location_id) {
    
            // TODO...
    
            return response;
    

    @ApiOperation is an annotation that comes from Swagger api, It is used for documenting the apis.

    0 讨论(0)
  • 2020-12-12 15:03

    To accept both @PathVariable and @RequestParam in the same /user endpoint:

    @GetMapping(path = {"/user", "/user/{data}"})
    public void user(@PathVariable(required=false,name="data") String data,
                     @RequestParam(required=false) Map<String,String> qparams) {
        qparams.forEach((a,b) -> {
            System.out.println(String.format("%s -> %s",a,b));
        }
      
        if (data != null) {
            System.out.println(data);
        }
    }
    

    Testing with curl:

    • curl 'http://localhost:8080/user/books'
    • curl 'http://localhost:8080/user?book=ofdreams&name=nietzsche'
    0 讨论(0)
  • 2020-12-12 15:21

    While the accepted answer by afraisse is absolutely correct in terms of using @RequestParam, I would further suggest to use an Optional<> as you cannot always ensure the right parameter is used. Also, if you need an Integer or Long just use that data type to avoid casting types later on in the DAO.

    @RequestMapping(value="/data", method = RequestMethod.GET)
    public @ResponseBody
    Item getItem(@RequestParam("itemid") Optional<Integer> itemid) { 
        if( itemid.isPresent()){
             Item i = itemDao.findOne(itemid.get());              
             return i;
         } else ....
    }
    
    0 讨论(0)
  • 2020-12-12 15:23

    I was interested in this as well and came across some examples on the Spring Boot site.

       // get with query string parameters e.g. /system/resource?id="rtze1cd2"&person="sam smith" 
    // so below the first query parameter id is the variable and name is the variable
    // id is shown below as a RequestParam
        @GetMapping("/system/resource")
        // this is for swagger docs
        @ApiOperation(value = "Get the resource identified by id and person")
        ResponseEntity<?> getSomeResourceWithParameters(@RequestParam String id, @RequestParam("person") String name) {
    
            InterestingResource resource = getMyInterestingResourc(id, name);
            logger.info("Request to get an id of "+id+" with a name of person: "+name);
    
            return new ResponseEntity<Object>(resource, HttpStatus.OK);
        }
    

    See here also

    0 讨论(0)
  • 2020-12-12 15:26

    Use @RequestParam

    @RequestMapping(value="user", method = RequestMethod.GET)
    public @ResponseBody Item getItem(@RequestParam("data") String itemid){
    
        Item i = itemDao.findOne(itemid);              
        String itemName = i.getItemName();
        String price = i.getPrice();
        return i;
    }
    
    0 讨论(0)
提交回复
热议问题