框架原理我了解的springbootjava所有基础知识

Springboot获取参数的几种类型

2018-12-20  本文已影响281人  海_做一个独立自由的人

    最近在学Springboot,发现有很多东西还是不一样,现在在此记录下,以便以后能对开发项目有帮助。

    使用Springboot,我们很多时候直接使用@PathVariable、@RequestParam、@Param、@RequestBody来获取参数,但是偶尔还是要用到request和response,怎么获取呢?

@PathVariable

是用来对url请求动态参数,如下代码,如只要type可以自定义动态参数。

@RequestMapping("/zyh/{type}")

  public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException {

    String url = "http://wx.diyfintech.com/zyhMain/" + type;

    if (type != 1 && type != 2) {

      throw new IllegalArgumentException("参数错误");

    }

    String encodeUrl = URLEncoder.encode(url, "utf-8");

    String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", ""));

    return "redirect:" + redirectUrl;

  }

@RequestParam 

这个注解有很多属性参数.下面我们来看下:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

public List<?> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

@Controller

@RequestMapping("/wx")

public class WxController {

    @Autowired

    private WxService wxService;

    private static final Log log= LogFactory.getLog(WxController.class);

    @RequestMapping(value = "/service",method = RequestMethod.GET)

    public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce,

                              @RequestParam String echostr, HttpServletResponse response) throws IOException {

        PrintWriter out = response.getWriter();

        if (SignUtil.checkSignature(signature, timestamp, nonce)) {

            out.print(echostr);

        }else

            out.print("fail");

        out.flush();

        out.close();

    }

@Param

    使用@Param需要加上("param参数名")。一般在xxControl类方法中尽量使用@RequestParam/@RequestBody,但是DAO层一般使用@Param。

@RequestBody

    对于这个接收的注解,我这边用的比较多,一般是由于需要接收多个参数,我会将这些参数打包成一个类VO,然后注解传过来,如下图:

此图私密,请勿外传

request

    对于request来接收数据,这里其实有三种方式来接收,任选其一就行:

1、通过静态方法获取,你也可以封装一个静态方法出来

@GetMapping(value ="")publicStringcenter(){    

    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();        HttpServletRequest request = servletRequestAttributes.getRequest();    HttpServletResponse response =     servletRequestAttributes.getResponse();//..

.}

2、通过参数直接获取,只要在你的方法上加上参数,Springboot就会帮你绑定,你可以直接使用。如果你的方法有其他参数,把这两个加到后面即可。

@GetMapping(value ="")publicStringcenter(HttpServletRequest request,HttpServletResponse response){//...}

3、注入到类,这样就不用每个方法都写了

@AutowiredprivateHttpServletRequest request;

@AutowiredprivateHttpServletResponse response;@GetMapping(value ="")

publicStringcenter(){//...}

我一般大多数习惯写成第二种方式。用request来接收有个好处就是可以接收任何类型的数据,比如文件流形式的接收。

文件流的接收需要使用到MultipartHttpServletRequest类(这里必须用这个类来接收)或者StandardMultipartHttpServletRequest

总结

    技术这东西就是需要多记,多写,多操作,没有其他捷径可走。

上一篇 下一篇

猜你喜欢

热点阅读