首页 > 解决方案 > 使用 Spring Boot 从 http 链接获取值

问题描述

我想从此 http 链接获取值:

https://www.test.com/notification?con=41280440000097&dp=1232&type=single

https://www.test.com/notification?con=41280440000097&sec=1232

我试图实现这个 Spring 代码:

    @PostMapping(value = "/v1/notification")
    public String handleNotifications(@RequestParam("notification") String itemid) {
        // parse here the values
    return "result";
    }

但是我如何解析 http 链接并在 HashMap 的 ArrayList 中获取值?

标签: javaspringspring-bootspring-rest

解决方案


你的控制器方法应该像这样RequestParam从请求中获取值,你可以使RequestParam optional withrequired=false and you can set default value also(@RequestParam(value = "i", defaultValue = "10") int i`

 @PostMapping(value = "/v1/notification")
public String handleNotifications(@RequestParam(value="con", required=false) String itemid, @RequestParam(value="dp", required=false) String dp, @RequestParam(value="type", required=false) String type ) 
    {
     // you can use all these values directly in this method
    // parse here the values
return "result";
}

推荐阅读