spring基礎---->請求與響應的參數(一)

來源:互聯網
上載者:User

標籤:birt   tty   經曆   部落格   說明   info   image   get請求   success   

  這裡面我們主要介紹一下spring中關於請求和響應參數資料的問題。愛,從來就是一件千迴百轉的事。不曾被離棄,不曾受傷害,怎懂得愛人?愛,原來是一種經曆。

 

spring中的請求與響應一、spring中json格式的post請求

用戶端發送json格式的資料,例如以下的user對象的資訊是我們要插入到資料庫中的。

{    "userId": "huhx1",    "username": "linux1",    "passowrd": "pwd11",    "sex": "0",    "phoneNumber": "15527100207",    "address": "wuhan1",    "birthday": "2017-12-12"}

 我們的controller處理方法中的參數可以用Bean接收,也可以使用Map。如下:

@PostMapping("user3")public ResponseBean<User> insertUser(@RequestBody User user) {    logger.info("user = {}", user);    userMapper.insertUser(user);    return ResultUtil.success(user);}@PostMapping("user4")public ResponseBean<Map<String, Object>> insertUser(@RequestBody Map<String, Object> user) {    logger.info("map = {}", user);    userMapper.insertMapUser(user);    return ResultUtil.success(user);}

  需要注意的是參數必須要用@RequestBody註解,否則請求的參數是不能正確的注入到參數的類中。關於這個註解和@ResponseBody差不多的功能,就是可以將我們的請求或者響應的資料通過HttpMessageConverters類轉換成我們想要的類型(被註解的類型)。官方文檔說明如下:

1、@RequestBody annotated parameters for access to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters.2、If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters.3、@RestController is a stereotype annotation that combines @ResponseBody and @Controller.

  

二、spring中表單格式的post請求

  如果是表單形式的post請求,那麼不需要@RequestBody註解。直接在controller對應方法的參數中註明該類就可以,在postman中如果birthday輸入的是2017-12-12。會報string不能轉換成date的錯,這裡面如果一定要支援2018-12-12字串格式的時間傳遞,可以使用spring自訂的converter或者formatter介面的實作類別。具體可以參考部落格:

上述測試的java代碼

@PostMapping("user3")public ResponseBean<User> insertUser(User user) {    logger.info("user = {}", user);    userMapper.insertUser(user);    return ResultUtil.success(user);}

 在使用JSON.toJSONString(this);格式化對象控制台中輸出的結果如下:

user = {"birthday":1513653098000,"password":"12345","phoneNumber":"2222","sex":1,"userId":"huhx3","username":"huhx3"}

我們返回的資料都是json格式的,這是因為我們在註解控制器的時候用的是@RestController註解。上述也提到過了,它是@Controller和@ResponseBody的複合註解。可以在它的源碼中找到:

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@ResponseBodypublic @interface RestController {    String value() default "";}

 

三、spring中表單的get請求
  • get傳遞參數的方式,我們的java代碼如下:
@GetMapping("getRequest1")public ResponseBean<String> getRequest1(String username) {    logger.info("username = {}", username);    return ResultUtil.success(username);}

我們通過postman發送get請求,url為:localhost:9998/mapper/getRequest1?username=huhx。返回的結果如下:

{    "returnCode": 0,    "returnMessage": "成功",    "response": "huhx"}

可以看到成功的接收到了get的請求參數,當然也可以使用@RequestParam修改參數的名稱。如下:

@GetMapping("getRequest1")public ResponseBean<String> getRequest1(@RequestParam("username") String name) {    logger.info("username = {}", name);    return ResultUtil.success(name);}
  • get請求路徑上的參數
@GetMapping("getRequest2/{username}")public ResponseBean<String> getRequest2(@PathVariable("username") String name) {    logger.info("username = {}", name);    return ResultUtil.success(name);}

如果像上述url參數的那樣,省略@PathVariable註解,使用下面的這種方式將會失效得不到傳遞的參數。

@GetMapping("getRequest2/{username}")public ResponseBean<String> getRequest2(String username) {    logger.info("username = {}", username);    return ResultUtil.success(username);}

 

 友情連結

 

spring基礎---->請求與響應的參數(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.