2014-10-20 83 views
2

這應該可能是一行代碼,但我不習慣Spring或SpringBoot,所以我遇到了麻煩。如何在GET請求中的休息控制器中獲取查詢參數?

我正在用查詢參數構建一個RESTful服務。例如:http://myweatherapi.com:8080/foo?zip=14325&prop=humidity

我想一個SpringBoot的模板中,我有此控制器:

@RestController 
public class ServiceController { 

    private static Logger LOG = LoggerFactory.getLogger(ServiceController.class); 

    @RequestMapping("/foo") 
    public String foo(@QueryParam("foo") String foo) { 
     requestContextDataService.addNamedParam("foo", foo); 

     // how can I access the full URL/query params here? 

     return "Service is alive!!"; 
    } 

} 

我的問題是:我如何可以訪問完整的URL /查詢參數

回答

6

下面是一個例子:

@RequestMapping("/foo") 
public String foo(HttpServletRequest request,@QueryParam("foo") String foo) { 
    requestContextDataService.addNamedParam("foo", foo); 

    // how can I access the full URL/query params here? 
    request.getRequestURL() 
    request.getQueryString() 

    return "Service is alive!!"; 
} 
相關問題