SpringBoot構(gòu)建Restful service完成Get和Post請(qǐng)求
一個(gè)基本的RESTfule service最進(jìn)場(chǎng)向外提供的請(qǐng)求Method就是Get和Post。
在Get中,常用的都會(huì)在請(qǐng)求上帶上參數(shù),或者是路徑參數(shù)。響應(yīng)Json。
在Post中,常用的會(huì)提交form data或者json data作為參數(shù),響應(yīng)Json。
1. Get請(qǐng)求,url傳參,返回json。
先準(zhǔn)備一個(gè)請(qǐng)求后,響應(yīng)的對(duì)象。
package com.example.demo; public class Echo { private final long id; private final String content; public Echo(long id, String content) { this.id = id; this.content = content; } public long getId() { return this.id; } public String getContent() { return this.content; } }
準(zhǔn)備一個(gè)用來接收請(qǐng)求的EchoController(名字可以根據(jù)實(shí)際情況寫),為它增加@RestController注解,表示這是一個(gè)處理RESTful請(qǐng)求的響處理類。
增加@RequestMapping,為本Controller提供一個(gè)根url,當(dāng)然可以不寫,寫這個(gè)是為了更好的將同一種處理的url歸在一類,本Controller其他的處理方法對(duì)應(yīng)的url中就不需要重復(fù)寫。
package com.example.demo; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ModelAttribute; @RestController @RequestMapping("/echo") public class EchoController { private static final String echoTemplate1 = "received %s!"; private static final String echoTemplate2 = "%s speak to %s \'%s\'"; private final AtomicLong counter = new AtomicLong(); @RequestMapping(value="/getter/pattern1", method=RequestMethod.GET) public Echo getterPattern1(String content) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content)); } @RequestMapping(value="/getter/pattern2", method=RequestMethod.GET) public Echo getterPattern2(@RequestParam(value="content", required=false) String alias) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, alias)); } }
getterPattern1的上面增加了@RequestMapping注解,將指定的url和處理的方法進(jìn)行映射,對(duì)應(yīng)這個(gè)url的請(qǐng)求就由該方法來處理,method可以省略,省略后就是對(duì)應(yīng)所有的Http Metho,gtterPatten1方法的參數(shù)默認(rèn)就和url中的content參數(shù)進(jìn)行映射。
再看getterPattern2,跟上面的方法效果一致,他們的區(qū)別就在于參數(shù)定義用了@RequestParam注解將url參數(shù)和方法參數(shù)進(jìn)行了映射說明,@RequesteParam中value的值就是url中實(shí)際的參數(shù),required說明該參數(shù)是否必須,如果是true,而實(shí)際上url中并沒有帶上該參數(shù),那么會(huì)報(bào)異常,為防止異??梢栽黾觗efaultValue指定參數(shù)的默認(rèn)值即可。我們會(huì)發(fā)現(xiàn)這里的方法參數(shù)是alias,這里當(dāng)然是可以和url的參數(shù)名不同,只要RequestParam注解映射了他們的關(guān)系就沒問題。
運(yùn)行后,可以在瀏覽器中訪問對(duì)應(yīng)的url來看看結(jié)果,我這里是用curl來訪問。
curl http://localhost:8080/echo/getter/pattern1?content=hello
curl http://localhost:8080/echo/getter/pattern2?content=hello
上面兩個(gè)url的訪問得到的結(jié)果除了id會(huì)自增外,其他是一致的:
{"id":6,"content":"received hello!"}
2. Get請(qǐng)求,傳遞url路徑參數(shù),返回json。
在EchoController中增加一個(gè)響應(yīng)方法。
@RequestMapping(value="/getter/pattern3/{content}", method=RequestMethod.GET) public Echo getterPattern3(@PathVariable String content) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content)); }
可以看到,在@RequestMapping的url定義中的末尾有“{content}”,表明這里是一個(gè)路徑參數(shù),在getterPattern3的參數(shù)content增加了@PathVariable注解,將方法參數(shù)與路徑參數(shù)進(jìn)行了映射。
運(yùn)行后,訪問url。
curl http://localhost:8080/echo/getter/pattern3/123456
結(jié)果:
{"id":8,"content":"received 123456!"}
3.Post請(qǐng)求,參數(shù)以Http body的途徑提交Json數(shù)據(jù)。
先定義一個(gè)提交的Json對(duì)應(yīng)的對(duì)象,這里把它定義為Message。
package com.example.demo; public class Message { private String from; private String to; private String content; public Message() {} public String getFrom() { return this.from; } public String getTo() { return this.to; } public String getContent() { return this.content; } public void setFrom(String value) { this.from = value; } public void setTo(String value) { this.to = value; } public void setContent(String value) { this.content = value; } }
在EchoController增加響應(yīng)的方法,并完成映射。
@RequestMapping(value="/setter/message1", method=RequestMethod.POST) public Echo setterMessage1(@RequestBody Message message) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent())); }
在setterMessage1方法的參數(shù)中用@RequestBody將請(qǐng)求的Http Body和參數(shù)messge進(jìn)行了映射。
運(yùn)行后,使用curl向服務(wù)端提交json數(shù)據(jù)。提交的請(qǐng)求頭部要帶上"Content-Type:application/json",表明請(qǐng)求體Json。
curl -i -H "Content-Type:application/json" -d "{\"from\":\"Tom\",\"to\":\"Sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1
結(jié)果:
{"id":9,"content":"Tom speak to Sandy 'hello buddy'"}
4.Post請(qǐng)求,參數(shù)以Http body的途徑提交表單數(shù)據(jù)。
在EchoController增加響應(yīng)的方法,并完成映射。
@RequestMapping(value="/setter/message2", method=RequestMethod.POST) public Echo setterMessage2(@ModelAttribute Message message) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent())); }
在setterMessage2方法的參數(shù)中用@ModelAttribute將請(qǐng)求的Http Body中的表單數(shù)據(jù)和參數(shù)messge進(jìn)行了映射。
運(yùn)行后,使用curl向服務(wù)端提交表單數(shù)據(jù)。提交的請(qǐng)求頭部要帶上"Content-Type:application/x-www-form-urlencoded",表明請(qǐng)求體是表單數(shù)據(jù),格式是"key1=value1&key2=value2&key3=value3"。
curl -i -H "Content-Type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2
結(jié)果:
{"id":11,"content":"sandy speak to aissen 'go to'"}
總結(jié)
以上所述是小編給大家介紹的SpringBoot構(gòu)建Restful service完成Get和Post請(qǐng)求,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java計(jì)算代碼段執(zhí)行時(shí)間的詳細(xì)過程
java里計(jì)算代碼段執(zhí)行時(shí)間可以有兩種方法,一種是毫秒級(jí)別的計(jì)算,另一種是更精確的納秒級(jí)別的計(jì)算,這篇文章主要介紹了java計(jì)算代碼段執(zhí)行時(shí)間,需要的朋友可以參考下2023-02-02Java中的日期和時(shí)間類以及Calendar類用法詳解
這篇文章主要介紹了Java中的日期和時(shí)間類以及Calendar類用法詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09Java中Arrays.asList()方法詳解及實(shí)例
這篇文章主要介紹了Java中Arrays.asList()方法將數(shù)組作為列表時(shí)的一些差異的相關(guān)資料,需要的朋友可以參考下2017-06-06Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03解決SpringAop內(nèi)部調(diào)用時(shí)不經(jīng)過代理類的問題
這篇文章主要介紹了解決SpringAop內(nèi)部調(diào)用時(shí)不經(jīng)過代理類的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01學(xué)習(xí)SpringBoot容器功能及注解原理
這篇文章主要介紹了學(xué)習(xí)SpringBoot容器功能及注解原理,文中通過詳細(xì)的代碼示例對(duì)SpringBoot容器功能及注解原理進(jìn)行了解析,有需要的朋友可以借鑒參考下2021-09-09Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法
這篇文章主要給大家介紹了關(guān)于Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們來一起看看吧2018-05-05Spring @Component自定義注解實(shí)現(xiàn)詳解
@Component是一個(gè)元注解,意思是可以注解其他類注解,如@Controller @Service @Repository @Aspect。官方的原話是:帶此注解的類看為組件,當(dāng)使用基于注解的配置和類路徑掃描的時(shí)候,這些類就會(huì)被實(shí)例化2022-09-09