SpringBoot獲取http請求參數的方法


SpringBoot獲取http請求參數的方法

原文:https://www.cnblogs.com/zhanglijun/p/9403483.html

有七種Java后台獲取前端傳來參數的方法,稍微羅列一下。

1. 直接把表單里面的參數寫進 Controller 相應方法的形參中去,這個獲取參數的方法適合get提交,而不適合post提交。

	/**
     * 1.直接把表單的參數寫在Controller相應的方法的形參中
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(String username,String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

url形式:http://localhost/jayvee/demo/addUser?username=wjw&password=123456 提交的參數名稱必須和Controller方法中定義的參數名稱一致。

2. 使用 HttpServletRequest 獲取參數,適用於post和get方法。

	/**
     * 2、通過HttpServletRequest接收
     * @param request
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(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。
首先創建一個和表單對應的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;
    }
}

使用創建的 bean 來封裝接收到前端傳來的參數

	/**
     * 3、通過一個bean來接收
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(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="/addUser/{username}/{password}",method=RequestMethod.GET)
public String addUser(@PathVariable String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

例如,訪問http://localhost/jayvee/demo/addUser4/wjw/123465時,則自動將URL中模板變量{username}和{password}綁定到通過@PathVariable注解的同名參數上,即入參后username=wjw、password=123465。

5. 使用@ModelAttribute注解獲取POST請求的FORM表單數據
jsp表單

<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post"> 
     用戶名:&nbsp;<input type="text" name="username"/><br/>
     密&nbsp;&nbsp;碼:&nbsp;<input type="password" name="password"/><br/>
     <input type="submit" value="提交"/> 
     <input type="reset" value="重置"/> 
</form>

Java Controller

	/**
     * 5、使用@ModelAttribute注解獲取POST請求的FORM表單數據
     * @param user
     * @return
     */
    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public String addUser(@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="/addUser",method=RequestMethod.GET)
public String addUser(@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="/addUser",method=RequestMethod.POST)
    public String addUser(@RequestBody UserDTO userDTO) {
        System.out.println("username is:"+userDTO.getUserName());
        System.out.println("password is:"+userDTO.getPassWord());
        return "demo/index";
    }

//UserDTO 這個類為一個實體類,里面定義的屬性與URL傳過來的屬性名一一對應。

以上為總結的七種后台springbooot獲取前端傳進參數的方法。

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。附上兩個例子,截圖如下:

4、get請求
在這里插入圖片描述
5、post請求
在這里插入圖片描述
另外,還有一種應用場景,接口規范為resultful風格時,舉個例子:如果要獲取某個id下此條問題答案的查詢次數的話,則后台就需要動態獲取參數, 其注解為@PathVariable,並且requestMapping中的value應為value="/{id}/queryNum",截圖如下:
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM