欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、接收 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)下面是一個簡單的測試樣例:

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

(1)如果沒有傳遞參數(shù)Controller將會報錯,這個同樣有如下兩種解決辦法:

  • 使用required = false標注參數(shù)是非必須的。
  • 使用defaultValue給參數(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;
    }
}

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

(1)Controller還可以直接使用map來接收所有的請求參數(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)下面是一個簡單的測試樣例:

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

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

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)下面是一個簡單的測試樣例:

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

1)如果一個post請求的參數(shù)太多,我們構(gòu)造一個對象來簡化參數(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ù)通過getter、setter方法注入到對象中去:

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)下面是一個簡單的測試樣例:

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

(5)如果一個 post 請求的參數(shù)分屬不同的對象,也可以使用多個對象來接收參數(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,使用對象接收時指定參數(shù)前綴

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

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

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

SpringBoot - @ControllerAdvice的使用詳解3(請求參數(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ā)送請求,可以看到參數(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)下面是一個簡單的測試樣例:

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

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

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)下面是一個簡單的測試樣例:

(4)如果傳遞的JOSN數(shù)據(jù)是一個數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring?webflux響應(yīng)式編程使用詳解

    spring?webflux響應(yīng)式編程使用詳解

    webflux,即響應(yīng)式編程,響應(yīng)式編程是一種用于處理異步數(shù)據(jù)流和事件的編程范式,spring?webflux是spring在5.0版本后提供的一套響應(yīng)式編程風(fēng)格的web開發(fā)框架,本文給大家詳細講講spring?webflux響應(yīng)式編程的使用,需要的朋友可以參考下
    2023-10-10
  • RocketMQ保證消息的有序性的案例分享

    RocketMQ保證消息的有序性的案例分享

    Apache RocketMQ 是一個常用的開源消息中間件,它提供了強大的有序消息處理能力,這里我們會探討 RocketMQ 是如何保證消息的有序性的,包括其設(shè)計原理和相關(guān)的源碼實現(xiàn),需要的朋友可以參考下
    2024-04-04
  • 如何在yml配置文件中使用中文注解

    如何在yml配置文件中使用中文注解

    這篇文章主要介紹了如何在yml配置文件中使用中文注解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java中的并發(fā)工具類詳細解析

    Java中的并發(fā)工具類詳細解析

    這篇文章主要介紹了Java中的并發(fā)工具類詳細解析,CountDownLatch、 CyclicBarrier 和 Semaphore 工具類提供了一種并發(fā)流程控制的手段,Exchanger 工具類則提供了在線程間交換數(shù)據(jù)的一種手段,需要的朋友可以參考下
    2023-12-12
  • Java如何使用Agent和ASM在字節(jié)碼層面實現(xiàn)方法攔截

    Java如何使用Agent和ASM在字節(jié)碼層面實現(xiàn)方法攔截

    Agent是一種運行在 Java 虛擬機 (JVM) 上的特殊程序,ASM是一個輕量級的 Java 字節(jié)碼編輯和分析框架,本文為大家介紹了如何利用他們在字節(jié)碼層面實現(xiàn)方法攔截,感興趣的可以了解一下
    2023-05-05
  • 詳解java整合solr5.0之solrj的使用

    詳解java整合solr5.0之solrj的使用

    本篇文章主要介紹了詳解java整合solr5.0之solrj的使用 ,具有一定的參考價值,有興趣的可以了解下
    2017-06-06
  • Spring MVC請求參數(shù)與響應(yīng)結(jié)果全局加密和解密詳解

    Spring MVC請求參數(shù)與響應(yīng)結(jié)果全局加密和解密詳解

    這篇文章主要給大家介紹了關(guān)于Spring MVC請求參數(shù)與響應(yīng)結(jié)果全局加密和解密的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • java存儲以及java對象創(chuàng)建的流程(詳解)

    java存儲以及java對象創(chuàng)建的流程(詳解)

    下面小編就為大家?guī)硪黄猨ava存儲以及java對象創(chuàng)建的流程(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 詳解mybatis-plus配置找不到Mapper接口路徑的坑

    詳解mybatis-plus配置找不到Mapper接口路徑的坑

    這篇文章主要介紹了詳解mybatis-plus配置找不到Mapper接口路徑的坑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 淺談java 面對對象(抽象 繼承 接口 多態(tài))

    淺談java 面對對象(抽象 繼承 接口 多態(tài))

    下面小編就為大家?guī)硪黄獪\談java 面對對象(抽象 繼承 接口 多態(tài))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論