美文网首页
spring的三种传参方式

spring的三种传参方式

作者: 初阳拾叁 | 来源:发表于2019-08-15 10:48 被阅读0次

    [TOC]

    1.get参数

      @RequestMapping(value = "/{appId}/url", method = RequestMethod.GET)
        @ResponseBody
        public String getParam1(@PathVariable Long appId, @RequestParam String url) {
            System.out.println(appId);
            System.out.println(url);
            return "redirect:" + StringEscapeUtils.unescapeHtml(url);
        }
    
    • 测试页面和返回
    image-20190815103207039

    2.body传输数据

      @RequestMapping(value = "/url", method = RequestMethod.POST)
        @ResponseBody
        public String getParam2(@RequestBody Param param) {
            System.out.println(param.getAppId());
            System.out.println(param.getUrl());
            return "redirect:" + StringEscapeUtils.unescapeHtml(param.getUrl());
        }
    
    
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Param {
        Long   appId;
        String url;
    }
    
    • 测试页面和返回
    image-20190815104307189

    3.form传输数据

        @RequestMapping(value = "/url", method = RequestMethod.GET)
        @ResponseBody
        public String getParam3(Param param) {
            System.out.println(param.getAppId());
            System.out.println(param.getUrl());
            return "redirect:" + StringEscapeUtils.unescapeHtml(param.getUrl());
        }
    
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Param {
        Long   appId;
        String url;
    }
    
    • 测试页面以及返回
    image-20190815104656744

    相关文章

      网友评论

          本文标题:spring的三种传参方式

          本文链接:https://www.haomeiwen.com/subject/rybajctx.html