SpringBoot 后端接收前端傳值的方法


1、通過HttpServletRequest接收,適用於GET 和 POST請求方式

      通過HttpServletRequest對象獲取請求參數

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping("/addUser2")
    public String addUser2(HttpServletRequest request, HttpServletResponse response) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

 2、SpringMVC,可以不設置任何注解即可接收參數

  1.SpringMVC,可以不設置任何注解即可接收參數,比如 
   @GetMapping("/category")
   public String category( Long id) {
    System.out.println(id);
    return "post/category";
}
  可以通過  /category 訪問 ,也可以通過 /category?id=1 訪問
2.SpringMVC ,也可以自動包裝成對象 
   url  /category?title=測試    或者  /category  都能訪問到目標資源
 
    @GetMapping("/category")
    public String category( MPost post ) {
        System.out.println(post.getTitle());
        return "post/category";
    }

多條參數的話可以這樣

 @GetMapping(value = "exportVisitorInfoList")
        public Book exportVisitorInfoList(String Code,String type,String startTime,String endTime){
                      Book book = new Book();
            book.setCompanyCode(Code);
            book.setType(type);
            book.setStartTime(startTime);
            book.setEndTime(endTime);
            return book;
        }

1.SpringMVC的自動封裝(不傳參也能進入)
@RequestParam(必須傳參,但可以手動設置為false)
@PathVariable(符合設定的正則表達式才允許進入,而且不能為空)
2.對比可知,主要是為了url提供更加嚴格的限制,以防止一些其他url進入該action。
3.提供復雜的接受參數的方式@RequestBody ,但必須將參數放置在@RequestBody中
4.針對PathVariable 需要注意的是參數中包含特殊字符的問題,可能導致參數不全。
5.對於各種請求方式,驗證一下當前用戶,對url進行加密 是有必要的。(尤其是關鍵數據)

 

3、通過@PathVariable獲取路徑中的參數,適用於GET請求

   通過注解獲取url參數

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
    public String addUser4(@PathVariable("username") String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";    }
}

測試代碼

<script>
    var xhr = new XMLHttpRequest()
    xhr.open('GET', 'http://localhost:8080/tools/addUser4/username=zhangsan/password=123') // 設置請求行
    xhr.send() 
    xhr.onload=function(){
      if(xhr.readyState!==4) return 
      console.log(xhr.responseText)
    }
  </script>

自動將URL中模板變量{username}和{password}綁定到通過@PathVariable注解的同名參數上,即入參后username=zhangsan、password=123

 4、用注解@RequestParam綁定請求參數到方法入參,適用於GET 和 POST請求方式

     添加@RequestParam注解,默認會校驗入參,如果請求不帶入參則會報錯,可以通過設置屬性required=false解決,例如: @RequestParam(value="username", required=false) ,這樣就不會校驗入參,於第一種請求方式一樣

@RestController
@RequestMapping("/tools")
public class InnerController {
    @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 "success";
    }
}

5、用注解@RequestBody綁定請求參數到方法入參 , 用於POST請求

@RequestBody主要用來接收前端傳遞給后端的json字符串中的數據的(請求體中的數據的)

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping(value="/addUser7",method=RequestMethod.POST)
    public String addUser7(@RequestBody DemoUser user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "success";
    }
}

測試代碼:

<script>
    var xhr = new XMLHttpRequest()
    xhr.open('POST', 'http://localhost:8080/tools/addUser7') // 設置請求行
    xhr.setRequestHeader('Content-Type','application/json')
    xhr.send('{"username":"zhangsan","password":"123"}') 
    xhr.onload=function(){
      if(xhr.readyState!==4) return 
      console.log(xhr.responseText)
    }
  </script>

 


免責聲明!

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



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