最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot接收?Form?表單數(shù)據(jù)的示例詳解

 更新時(shí)間:2022年08月09日 11:13:04   作者:一個(gè)渺小的人  
這篇文章主要介紹了Springboot接收?Form?表單數(shù)據(jù)的實(shí)例代碼,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、接收 Form 表單數(shù)據(jù)

1,基本的接收方法

(1)下面樣例Controller接收form-data格式的POST數(shù)據(jù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello1")
    public String postHello1(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
        return "name:" + name + "\nage:" + age;
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

2,參數(shù)沒有傳遞的情況

(1)如果沒有傳遞參數(shù)Controller將會(huì)報(bào)錯(cuò),這個(gè)同樣有如下兩種解決辦法:

  • 使用required = false標(biāo)注參數(shù)是非必須的。
  • 使用defaultValue給參數(shù)指定個(gè)默認(rèn)值。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello2")
    public String postHello2(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
        return "name:" + name + "\nage:" + age;
    }
}

3,使用 map 來接收參數(shù)

(1)Controller還可以直接使用map來接收所有的請(qǐng)求參數(shù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello2")
    public String postHello2(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
        return "name:" + name + "\nage:" + age;
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

4,接收一個(gè)數(shù)組

(1)表單中有多個(gè)同名參數(shù),Controller這邊可以定義一個(gè)數(shù)據(jù)進(jìn)行接收:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    @PostMapping("/postHello4")
    public String postHello4(@RequestParam("name") String[] names) {
        String result = "";
        for(String name:names){
            result += name + "\n";
        }
        return result;
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

5,使用對(duì)象來接收參數(shù)

1)如果一個(gè)post請(qǐng)求的參數(shù)太多,我們構(gòu)造一個(gè)對(duì)象來簡(jiǎn)化參數(shù)的接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello5")
    public String postHello5(User user) {
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
}

(2)User類的定義如下,到時(shí)可以直接將多個(gè)參數(shù)通過getter、setter方法注入到對(duì)象中去:

public class User {
    private String name;
    private Integer age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

(3)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

(4)如果傳遞的參數(shù)有前綴,且前綴與接收實(shí)體類的名稱相同,那么參數(shù)也是可以正常傳遞的:

(5)如果一個(gè) post 請(qǐng)求的參數(shù)分屬不同的對(duì)象,也可以使用多個(gè)對(duì)象來接收參數(shù):

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello5-1")
    public String hello(User user, Phone phone) {
        return "name:" + user.getName() + "\nage:" + user.getAge()
                + "\nnumber:" + phone.getNumber();
    }
}

6,使用對(duì)象接收時(shí)指定參數(shù)前綴

(1)如果傳遞的參數(shù)有前綴,且前綴與接收實(shí)體類的名稱不同相,那么參數(shù)無法正常傳遞:

(2)我們可以結(jié)合@InitBinder解決這個(gè)問題,通過參數(shù)預(yù)處理來指定使用的前綴為 u.

除了在 Controller 里單獨(dú)定義預(yù)處理方法外,我們還可以通過@ControllerAdvice結(jié)合@InitBinder來定義全局的參數(shù)預(yù)處理方法,方便各個(gè)Controller使用。具體做法參考我之前的文章:

SpringBoot - @ControllerAdvice的使用詳解3(請(qǐng)求參數(shù)預(yù)處理 @InitBinder)

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class HelloController {
    @PostMapping("/postHello6")
    public String postHello6(@ModelAttribute("u") User user) {
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
 
    @InitBinder("u")
    private void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("u.");
    }
}

(3)重啟程序再次發(fā)送請(qǐng)求,可以看到參數(shù)已經(jīng)成功接收了:

二、接收字符串文本數(shù)據(jù)

(1)如果傳遞過來的是Text文本,我們可以通過HttpServletRequest獲取輸入流從而讀取文本內(nèi)容。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
@RestController
public class HelloController {
    @PostMapping("/helloText")
    public String hello(HttpServletRequest request) {
        ServletInputStream is = null;
        try {
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len));
            }
            System.out.println(sb.toString());
            return "獲取到的文本內(nèi)容為:" + sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

三、接收 JSON 數(shù)據(jù)

1,使用 Map 來接收數(shù)據(jù)

(1)如果把json作為參數(shù)傳遞,我們可以使用@requestbody接收參數(shù),將數(shù)據(jù)轉(zhuǎn)換Map:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/helloBean")
    public String hello(@RequestBody User user){
        return user.getName() + " " + user.getAge();
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

2,使用 Bean 對(duì)象來接收數(shù)據(jù)

(1)如果把json作為參數(shù)傳遞,我們可以使用@requestbody接收參數(shù),將數(shù)據(jù)直接轉(zhuǎn)換成對(duì)象:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/helloBean")
    public String hello(@RequestBody User user){
        return user.getName() + " " + user.getAge();
    }
}

(2)下面是一個(gè)簡(jiǎn)單的測(cè)試樣例:

(4)如果傳遞的JOSN數(shù)據(jù)是一個(gè)數(shù)組也是可以的,Controller做如下修改:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class HelloController {
    @PostMapping("/helloList")
    public String helloList(@RequestBody List<User> users){
        String result = "";
        for(User user:users){
            result += user.getName() + " " + user.getAge() + "\n";
        }
        return result;
    }
}

到此這篇關(guān)于Springboot接收 Form 表單數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Springboot接收表單數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

寿宁县| 河间市| 华宁县| 咸丰县| 鱼台县| 丽水市| 安仁县| 江北区| 桦甸市| 凤凰县| 农安县| 石家庄市| 闵行区| 科技| 永靖县| 宜宾县| 阿拉善右旗| 阿拉善右旗| 大洼县| 大石桥市| 仁化县| 青铜峡市| 四子王旗| 怀来县| 铁力市| 丹寨县| 囊谦县| 绥阳县| 于都县| 泰顺县| 孟津县| 锡林郭勒盟| 冀州市| 宝兴县| 玉田县| 定安县| 安吉县| 益阳市| 仁布县| 宜城市| 临高县|