One - One Code All

Blog Content

springboot(服务端接口)获取URL请求参数的几种方法

Java   2020-04-21 22:33:48

原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html


一、下面为7种服务端获取前端传过来的参数的方法 

  常用的方法为:@RequestParam和@RequestBody

1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。

    /**
     * 1.直接把表单的参数写在Controller相应的方法的形参中
      * @param username
     * @param password
     * @return
     */
    @RequestMapping("/addUser1")
    public String addUser1(String username,String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的参数需要和Controller方法中的入参名称一致。


2、通过HttpServletRequest接收,post方式和get方式都可以。

 /**
     * 2、通过HttpServletRequest接收
      * @param request
     * @return
     */
    @RequestMapping("/addUser2")
    public String addUser2(HttpServletRequest request) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

3、通过一个bean来接收,post方式和get方式都可以。

(1)建立一个和表单中参数对应的bean

package demo.model;

public class UserModel {
    
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

(2)用这个bean来封装接收的参数

 /**
     * 3、通过一个bean来接收
      * @param user
     * @return
     */
    @RequestMapping("/addUser3")
    public String addUser3(UserModel user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "demo/index";
    }

4、通过@PathVariable获取路径中的参数

/**
     * 4、通过@PathVariable获取路径中的参数
      * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

例如,访问http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。

5、使用@ModelAttribute注解获取POST请求的FORM表单数据

Jsp表单如下:

 

     用户名: 

     密  码: 

      

      

 


Java Controller如下:

 /**
     * 5、使用@ModelAttribute注解获取POST请求的FORM表单数据
      * @param user
     * @return
     */
    @RequestMapping(value="/addUser5",method=RequestMethod.POST)
    public String addUser5(@ModelAttribute("user") UserModel user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "demo/index";
    }

6、用注解@RequestParam绑定请求参数到方法入参


当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)

 /**
     * 6、用注解@RequestParam绑定请求参数到方法入参
      * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/addUser6",method=RequestMethod.GET)
    public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

7、用注解@RequestBody绑定请求参数到方法入参  用于POST请求

/**
     * 7、用注解@Requestbody绑定请求参数到方法入参
      * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/addUser6",method=RequestMethod.POST)
    public String addUser6(@RequestBody UserDTO userDTO) {
        System.out.println("username is:"+userDTO.getUserName());
        System.out.println("password is:"+userDTO.getPassWord());
        return "demo/index";
    }

//UserDTO 这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。

 

上面为7种服务端获取前端传过来的参数的方法。


二、spring boot的@RequestParam和@RequestBody的区别

1、问题描述

      由于项目是前后端分离,因此后台使用的是spring boot,做成微服务,只暴露接口。接口设计风格为restful的风格,在get请求下,后台接收参数的注解为RequestBody时会报错;在 post请求下,后台接收参数的注解为RequestParam时也会报错。


2、问题原因


     由于spring的RequestParam注解接收的参数是来自于requestHeader中,即请求头,也就是在url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。


3、解决方法


      因此综上所述,如果为get请求时,后台接收参数的注解应该为RequestParam,如果为post请求时,则后台接收参数的注解就是为RequestBody。


        另外,还有一种应用场景,接口规范为resultful风格时,举个例子:如果要获取某个id下此条问题答案的查询次数的话,则后台就需要动态获取参数, 其注解为@PathVariable,并且requestMapping中的value应为value="/{id}/queryNum"。



三、SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@RequestParam/@GetMapping。


其中,各注解的作用为:


@PathVaribale 获取url中的数据


@RequestParam 获取请求参数的值


@GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写


@PathVaribale 获取url中的数据


同样,如果我们需要在url有多个参数需要获取,则如下代码所示来做就可以了。


@RestController

public class HelloController {


    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)

    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){

        return "id:"+id+" name:"+name;

    }

}



以上,通过@PathVariable注解来获取URL中的参数时的前提条件是我们知道url的格式时怎么样的。


只有知道url的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。


一般情况下,url的格式为:localhost:8080/hello?id=98,这种情况下该如何来获取其id值呢,这就需要借助于@RequestParam来完成了


@RequestParam 获取请求参数的值

直接看一个例子,如下


@RestController

public class HelloController {


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

    public String sayHello(@RequestParam("id") Integer id){

        return "id:"+id;

    }

}


@RequestParam注解给我们提供了这种解决方案,即允许用户不输入id时,使用默认值,具体代码如下:


@RestController

public class HelloController {

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

    //required=false 表示url中可以不穿入id参数,此时就使用默认参数

    public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){

        return "id:"+id;

    }

}


如果在url中有多个参数,即类似于localhost:8080/hello?id=98&&name=wojiushimogui这样的url,同样可以这样来处理。


@GetMapping 组合注解

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。


即可以使用@GetMapping(value = “/hello”)来代替@RequestMapping(value=”/hello”,method= RequestMethod.GET)。即可以让我们精简代码。


例子

@RestController
public class HelloController {
    //@RequestMapping(value="/hello",method= RequestMethod.GET)
    @GetMapping(value = "/hello")
    //required=false 表示url中可以不穿入id参数,此时就使用默认参数
    public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
        return "id:"+id;
    }
}




上一篇:elasticsearch查询opendistrosql支持的操作
下一篇:opendistro sql的二次开发

The minute you think of giving up, think of the reason why you held on so long.