本文預(yù)計需要10分鐘閱讀
專題簡介
SpringBoot之路專題是一個記錄本人在使用Spring和SpringBoot相關(guān)技術(shù)中所遇到的問題和要解決的問題翰舌。每用到一處知識點,就會把這處知識補充到Github一個對應(yīng)的分支上。會以專題的方式舞骆,力爭每一篇博客,由淺入深,把每個知識點講解到實戰(zhàn)級別缸逃,并且分析Spring源碼。整個項目會以一個開發(fā)一個博客系統(tǒng)為最終目標厂抽,每一個分支都記錄著一步一步搭建的過程需频。與大家分享,代碼會同步發(fā)布到這里筷凤。
本節(jié)簡介
在上一節(jié)http://www.reibang.com/p/ecf712f4399f 介紹了基礎(chǔ)的參數(shù)綁定使用方式贺辰。本節(jié)將介紹進階的一些知識點,包括具體的@RequestParam
參數(shù)設(shè)置和自定義參數(shù)綁定支持嵌施。
package com.beenoisy.springboot.way.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController// 1
@RequestMapping(value = "article") // 2
public class ArticleController {
@RequestMapping(method = RequestMethod.GET) // 3
public Map<String, String> getArticle(
@RequestParam("id") int id// 4
) {
Map<String, String> result = new HashMap<>(); // 5
result.put("title", "this is article of id " + id); // 5
result.put("content", "this is article of id " + id); // 5
return result;
}
}
上一節(jié)的代碼中饲化,4
這一步對參數(shù)int id
加上了@RequestParam("id")
注解,使其可以對url中名為id的query進行映射吗伤。@RequestParam
還提供了更多的參數(shù)可供選擇吃靠,其完整的參數(shù)簽名如下:
@RequestParam(
value = "id", // 1
required = true, // 2
defaultValue = "0", // 3
name = "id" // 4
) int id
- 要綁定的請求參數(shù)名
- 是否必選
- 默認值(
required
為false
時生效) -
value
的別名,含義和value
一致
自定義參數(shù)綁定
講一個具體的場景足淆,比如巢块,我們需要分頁獲取一個數(shù)據(jù),每次都需要傳pageNo和pageSize兩個參數(shù)巧号,某種程度上來講族奢,是一種浪費〉ず瑁可否自己定義一個參數(shù)解析器越走,對page
類型的參數(shù)進行解析呢?
需要自己定義一個convert并將其加入到spring容器中靠欢。
首先廊敌,實現(xiàn)pager源碼:
package com.beenoisy.springboot.way.common.config.converter;
public class Pager {
private int pageNo;
private int pageSize;
public Pager(int pageNo, int pageSize) {
this.pageNo = pageNo;
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public int getPageSize() {
return pageSize;
}
@Override
public String toString() {
return "Pager{" +
"pageNo=" + pageNo +
", pageSize=" + pageSize +
'}';
}
}
可以看出,pager只是一個普通java類(pojo)门怪,并沒有什么特殊的設(shè)置骡澈。
下面來看convert代碼:
package com.beenoisy.springboot.way.common.config.converter;
import org.springframework.core.convert.converter.Converter;
/**
* pager object, format as :"pageNo,pageSize"
* Created by jacks808@163.com on 16/9/15.
*/
public class PagerConverter implements Converter<String, Pager> {// 1
public static final int PAGE_SIZE = 20;
@Override
public Pager convert(String s) {
// 沒有給正確的參數(shù)則取第一頁
if (s == null || s.length() == 0) {
return new Pager(0, PAGE_SIZE);
}
String[] pagerSplit = s.split(",");
if (pagerSplit.length != 2) {
throw new IllegalStateException("can't parse '" + s + "' to pager, please input pager as pageNo,pageSize");
}
try {
int pageNo = Integer.parseInt(pagerSplit[0]);
int pageSize = Integer.parseInt(pagerSplit[1]);
return new Pager(pageNo, pageSize); // 2
} catch (NumberFormatException e) {
throw new IllegalStateException("can't parse '" + s + "' to pager, please input pager as pageNo,pageSize");
}
}
}
- 實現(xiàn)converter接口,這個接口包含了convert方法掷空。Converter的參數(shù)簽名中肋殴,有兩個泛型參數(shù)囤锉,<FROM, TO>第一個參數(shù)是原參數(shù),通常均為String护锤,第二個參數(shù)就是目標參數(shù)類型嚼锄,這里為Pager
- 進行一些參數(shù)處理后,返回pager對象蔽豺。
配置類:
package com.beenoisy.springboot.way.common.config.converter;
import com.beenoisy.springboot.way.common.config.converter.support.PagerConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Created by jacks808@163.com on 16/9/15.
*/
@Configuration
public class ConvertConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {// 1
registry.addConverter(new PagerConverter());// 2
}
}
- 覆蓋
addFormatters
方法 - 將自己實現(xiàn)的convert加入到registry中区丑。
Controller使用時的代碼:
package com.beenoisy.springboot.way.controller;
import com.beenoisy.springboot.way.common.config.converter.support.Pager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "article")
public class ArticleController {
@RequestMapping(method = RequestMethod.GET)
public String getArticles(
@RequestParam("pager") Pager pager// 1
) {
int pageNo = pager.getPageNo();
int pageSize = pager.getPageSize();
return "pageNo is: " + pageNo + ", pageSize is: " + pageSize;
}
}
- 直接使用pager對象作為參數(shù),這時springmvc\spring boot會自動調(diào)用已經(jīng)加在配置里的pagerConverter進行參數(shù)綁定修陡。
運行截圖
這了pager
參數(shù)為1,5
按照解析規(guī)則沧侥,會把1
解析為page No,5
解析為page size魄鸦。
最后宴杀,源碼放在這里:
https://github.com/jacks808/spring-boot-way/tree/08-customer-param-binding
如果文章內(nèi)容對你有幫助,歡迎在github上star或點贊拾因。