【问题标题】:Get values with Spring Boot from http link使用 Spring Boot 从 http 链接获取值
【发布时间】:2019-01-13 20:01:24
【问题描述】:

我想从这个 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 中的值?

【问题讨论】:

标签: java spring spring-boot spring-rest


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但是如果我有 2 个键和值怎么办?还有其他方法可以解析它们吗?
  • 你给我一个键con=41280440000097,1234566&dp=1232&type=single这样的两个值吗?
  • 找不到需要的参数怎么办?会抛出错误吗?
  • 它将为空,没有错误,如果你不想为空设置默认值
  • 请您扩展帖子。还有我可以添加的任何安全性吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 2010-12-13
  • 1970-01-01
  • 2018-08-16
  • 2020-05-21
  • 2017-11-06
  • 2018-09-15
  • 2016-08-16
相关资源
最近更新 更多