国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102)

這篇具有很好參考價值的文章主要介紹了【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

請求參數(shù)傳json對象,后端采用(pojo)接收的前提條件:

1.pom.xml文件加入坐標(biāo)依賴:jackson-databind
2.Spring Boot 的啟動類加注解:@EnableWebMvc
3.Spring Boot 的Controller接受參數(shù)采用:@RequestBody
4.postman入?yún)⒉捎胘son格式

1.pom.xml文件加入坐標(biāo):


<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

2.Spring Boot 啟動類:加注解:@EnableWebMvc


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableWebMvc
@EnableFeignClients(basePackages = {
        "com.itheima.platform.common.feign.services.home",
        "com.itheima.bcg.service.fegin",
        "com.itheima.platform.common.feign.services.paperTask",
        "com.itheima.platform.common.feign.services.problem",
        "com.itheima.platform.common.feign.services.auth",
		"com.itheima.platform.common.feign.services.bpmn"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

	@Bean
	public RestTemplate restTemplate() {
		//復(fù)雜構(gòu)造函數(shù)的使用
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(300000);// 設(shè)置超時
		requestFactory.setReadTimeout(300000);
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.setRequestFactory(requestFactory);
		return restTemplate;
	}

	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setConnectTimeout(150000);
		factory.setReadTimeout(50000);
		return factory;
	}
}

POJO類:


import lombok.Data;

@Data
public class Config {
    private int seqId;
    private int sortId;
    private String interfaceType;
    private String dicNameFirst;
    private int dicValueFirst;
    private String dicNameSecond;
    private int dicValueSecond;
    private String dicType;
    private String isEnable;
}

3.Controller接受參數(shù)采用:@RequestBody
Controller類:
備注:為了便于測試:Controller類只寫了一個接口(實際開發(fā)可不要這樣寫噢)


	/*
     * 請求參數(shù)傳json對象(POJO)
     *
     */
    @PostMapping(value = "/addTest")
    @AuthInterceptor("mg:get:addTest")
    public Result addTest(@RequestBody Config config) {
        try {
            return xxxListService.addTest(config);
        } catch (Exception e) {
            log.error("Controller addTest is error===:" + e.getMessage(), e);
            return Result.failure("測試成功");
        }
    }
    

Service類:

	Result addTest(Config config);

ServiceImpl類:


 	@Override
    public Result addTest(Config config) {
        List<Map<String, Object>> res = new ArrayList<>();
        String interfaceType = config.getInterfaceType();
        if(interfaceType.equals("add")){
            xxxListMapper.addTest(config);
        }
        else if(interfaceType.equals("del")){
            xxxListMapper.delTest(config);
        }
        else if(interfaceType.equals("modify")){
            xxxListMapper.modifyTest(config);
        }
        else if(interfaceType.equals("sel")){
            res = xxxListMapper.selTest(config);
        }
        return Result.success().result(res);
    }
    

Mapper類:

	
	//新增
    void addTest(Config config);
    //刪除
    void delTest(Config config);
    //修改
    void modifyTest(Config config);
    //查詢
    List<Map<String, Object>> selTest(Config config);
    

Mapper.xml類


	<!-- 新增 -->
    <insert id="addTest">
        INSERT IGNORE INTO xxx_other_list_dic
        (dicNameFirst,dicValueFirst,dicNameSecond,dicValueSecond,dicType,isEnable)
        VALUES
        (#{dicNameFirst},#{dicValueFirst},#{dicNameSecond},#{dicValueSecond},#{dicType},#{isEnable})
    </insert>
    <!-- 刪除 -->
    <select id="delTest">
        delete
        FROM xxx_other_list_dic where
        <if test = "null != seqId and '' != seqId">
            seqId = #{seqId}
        </if>
    </select>
    <!-- 修改 -->
    <update id="modifyTest">
        update xxx_other_list_dic
        <set>
            <if test = "null != sortId and '' != sortId">
                sortId = #{sortId},
            </if>
            <if test = "null != isEnable and '' != isEnable">
                isEnable = #{isEnable}
            </if>
        </set>
        where
        <if test = "null != seqId and '' != seqId">
            seqId = #{seqId}
        </if>
    </update>
    <!-- 查詢 -->
    <select id="selTest" resultType="map">
        SELECT *
        FROM xxx_other_list_dic where 1 = 1
        <if test="null != dicNameFirst and '' != dicNameFirst">
            and dicNameFirst = #{dicNameFirst}
        </if>
        <if test="null != dicValueFirst and '' != dicValueFirst">
            and dicValueFirst = #{dicValueFirst}
        </if>
        <if test="null != dicNameSecond and '' != dicNameSecond">
            and dicNameSecond = #{dicNameSecond}
        </if>
        <if test="null != dicValueSecond and '' != dicValueSecond">
            and dicValueSecond = #{dicValueSecond}
        </if>
        <if test="null != dicType and '' != dicType">
            and dicType = #{dicType}
        </if>
        <if test="null != isEnable and '' != isEnable">
            and isEnable = #{isEnable}
        </if>
        order by sortId
    </select>
    

4.postman入?yún)⒉捎胘son格式
postman 接口測試:
新增:
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
修改:

【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
查詢:
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
刪除:
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java
【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102),框架,java,spring boot,json,java文章來源地址http://www.zghlxwxcb.cn/news/detail-624365.html

到了這里,關(guān)于【Spring Boot】請求參數(shù)傳json對象,后端采用(pojo)CRUD案例(102)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • vue使用axios發(fā)送post請求攜帶json body參數(shù),后端使用@RequestBody進行接收

    vue使用axios發(fā)送post請求攜帶json body參數(shù),后端使用@RequestBody進行接收

    最近在做自己項目中,做一個非常簡單的新增用戶場景,但是使用原生axios發(fā)送post請求的時候,還是踩了不少坑的。 唉,說多了都是淚,小小一個新增業(yè)務(wù),在自己前后端一起開發(fā)的時候,硬是搞了好久。 下面就把問題總結(jié)分享下,防止后人再踩坑。 首先先看下我的接口定

    2024年02月02日
    瀏覽(24)
  • Spring-Cloud-Gateway修改請求(json,form帶文件請求)參數(shù),返回值參數(shù)

    新項目需要在getway統(tǒng)一做入?yún)?、出參加解密,記錄日志。記錄一下form,x-www-form-urlencoded , json 這幾種修改數(shù)據(jù)的方式。 gateway做攔截器是實現(xiàn)GlobalFilter接口,修改json方式網(wǎng)上有很多文章,后來又想研究研究能不能實現(xiàn)修改form-data參數(shù),以及文件請求,后者文章不多大部分是怎

    2024年02月16日
    瀏覽(29)
  • Spring Boot請求處理-常用參數(shù)注解

    Spring Boot請求處理-常用參數(shù)注解

    @PathVariable 路徑變量 @RequestParam 獲取請求參數(shù) @RequestHeader 獲取請求頭 @RequestBody 獲取請求體【Post】 @CookieValue 獲取Cookie值 RequestAttribute 獲取request域?qū)傩?@ModelAttribute 1. @PathVariable 該注解主要用于rest風(fēng)格的搭配使用,請求路徑中不再以 k:v 的形式給出請求參數(shù)和值;而是直接給定

    2024年02月10日
    瀏覽(33)
  • Spring MVC -- 獲取參數(shù)(普通對象+JSON對象+URL地址參數(shù)+文件+Cookie/Session/Header)

    Spring MVC -- 獲取參數(shù)(普通對象+JSON對象+URL地址參數(shù)+文件+Cookie/Session/Header)

    目錄 1.獲取參數(shù) 1.1獲取單個參數(shù) 1.2獲取多個參數(shù) 傳參注意事項: 2.獲取對象 ?3.后端參數(shù)重命名@RequestParam 4.獲取JSON對象@RequestBody 5.從 URL 地址中獲取參數(shù) @PathVariable 6.上傳文件 @RequestPart 7.獲取Cookie/Session/Header 7.1 獲取 Request 和 Response 對象 7.2 獲取Cookie (1)Servlet 獲取 Cooki

    2024年02月16日
    瀏覽(26)
  • Spring Boot中處理前端的POST請求參數(shù)

    在Spring Boot中處理前端的POST請求參數(shù)可以使用@RequestParam注解或@RequestBody注解。 @RequestParam注解用于獲取請求參數(shù)的值,可以用于處理GET和POST請求。它可以指定參數(shù)的名稱、是否必須、默認(rèn)值等屬性。 例如,假設(shè)前端發(fā)送了一個POST請求,請求參數(shù)為name和age,可以使用@Request

    2024年02月15日
    瀏覽(22)
  • Spring Boot學(xué)習(xí)筆記(十三)獲取HTTP請求參數(shù)

    SpringBoot獲取參數(shù)代碼 User類: (1)路徑參數(shù) 使用@PathVariable獲取 (2)URL參數(shù) 1、使用@RequestParam獲取 2、使用request的getParameter方法獲取 3、直接在controller里使用同名的方法參數(shù)獲取 4、使用實體類的同名屬性接收參數(shù) (3)表單參數(shù) 獲取方式Url參數(shù)一樣獲取。GET請求只能拿到

    2024年02月04日
    瀏覽(22)
  • Spring Boot中使用JSR-303實現(xiàn)請求參數(shù)校驗

    Spring Boot中使用JSR-303實現(xiàn)請求參數(shù)校驗

    JSR-303是Java中的一個規(guī)范,用于實現(xiàn)請求參數(shù)校驗。它定義了一組注解,可以應(yīng)用于JavaBean的字段上,用于驗證輸入?yún)?shù)的合法性。下面是一些常用的JSR-303注解及其介紹: @NotNull :用于驗證字段值不能為null。 @NotEmpty :用于驗證字符串字段不能為空。 @NotBlank :用于驗證字符

    2024年02月08日
    瀏覽(23)
  • spring-boot 請求參數(shù)校驗:注解 @Validated 的使用、手動校驗、自定義校驗

    spring-boot中可以用@validated來校驗數(shù)據(jù),如果數(shù)據(jù)異常則會統(tǒng)一拋出異常,方便異常中心統(tǒng)一處理。 spring-boot已經(jīng)引入了基礎(chǔ)包,所以直接使用就可以。 在屬性上添加校驗注解: 在Controller上添加 @Validated 注解 校驗未通過時,可能看到: 在 @Validated 后面緊跟著追加BindingResult,

    2023年04月16日
    瀏覽(33)
  • spring boot 單元測試JUnit5使用MockMvc調(diào)用get請求,post請求,設(shè)置head請求頭,解析返回值json和字符串

    spring boot 單元測試JUnit5使用MockMvc調(diào)用get/post接口 源碼地址:https://gitcode.net/qq_39339588/springboot.git 1. 先準(zhǔn)備一份controller,一會兒供測試調(diào)用 2. MockMvc測試調(diào)用get請求接口 兩個注解說明 @SpringBootTest // 加測試類上,標(biāo)明是測試的類 @AutoConfigureMockMvc // 支持對MockMvc對象的注入和配置,

    2024年02月14日
    瀏覽(24)
  • Spring Boot框架中Controller層API接口如何支持使用多個@RequestBody注解接受請求體參數(shù)

    Spring Boot框架中Controller層API接口如何支持使用多個@RequestBody注解接受請求體參數(shù)

    眾所周知,在Spring Boot框架中,Controller層API接口編碼獲取請求體參數(shù)時,在參數(shù)上會使用@RequestBody注解;如果一次請求中,請求體參數(shù)攜帶的內(nèi)容需要用多個參數(shù)接收時,能不能多次使用@RequestBody注解呢? 下面我們先測試一下,參考代碼: PostMan進行請求: 服務(wù)端后端日志:

    2024年01月17日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包