一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot構建Restful service完成Get和Post請求

SpringBoot構建Restful service完成Get和Post請求

2020-12-17 14:45白色的海 Java教程

這篇文章主要介紹了SpringBoot構建Restful service完成Get和Post請求的示例代碼,感興趣的朋友一起看看吧

一個基本的RESTfule service最進場向外提供的請求Method就是GetPost

在Get中,常用的都會在請求上帶上參數,或者是路徑參數。響應Json。

在Post中,常用的會提交form data或者json data作為參數,響應Json。

1. Get請求,url傳參,返回json。

先準備一個請求后,響應的對象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
  }
}

準備一個用來接收請求的EchoController(名字可以根據實際情況寫),為它增加@RestController注解,表示這是一個處理RESTful請求的響處理類。

增加@RequestMapping,為本Controller提供一個根url,當然可以不寫,寫這個是為了更好的將同一種處理的url歸在一類,本Controller其他的處理方法對應的url中就不需要重復寫。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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和處理的方法進行映射,對應這個url的請求就由該方法來處理,method可以省略,省略后就是對應所有的Http Metho,gtterPatten1方法的參數默認就和url中的content參數進行映射。

再看getterPattern2,跟上面的方法效果一致,他們的區別就在于參數定義用了@RequestParam注解將url參數和方法參數進行了映射說明,@RequesteParam中value的值就是url中實際的參數,required說明該參數是否必須,如果是true,而實際上url中并沒有帶上該參數,那么會報異常,為防止異常可以增加defaultValue指定參數的默認值即可。我們會發現這里的方法參數是alias,這里當然是可以和url的參數名不同,只要RequestParam注解映射了他們的關系就沒問題。

運行后,可以在瀏覽器中訪問對應的url來看看結果,我這里是用curl來訪問。

curl http://localhost:8080/echo/getter/pattern1?content=hello
curl http://localhost:8080/echo/getter/pattern2?content=hello

 上面兩個url的訪問得到的結果除了id會自增外,其他是一致的:

?
1
{"id":6,"content":"received hello!"}

 2. Get請求,傳遞url路徑參數,返回json。

在EchoController中增加一個響應方法。

?
1
2
3
4
@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}”,表明這里是一個路徑參數,在getterPattern3的參數content增加了@PathVariable注解,將方法參數與路徑參數進行了映射。

運行后,訪問url。

curl http://localhost:8080/echo/getter/pattern3/123456

結果:

?
1
{"id":8,"content":"received 123456!"}

3.Post請求,參數以Http body的途徑提交Json數據。

先定義一個提交的Json對應的對象,這里把它定義為Message。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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增加響應的方法,并完成映射。

?
1
2
3
4
@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方法的參數中用@RequestBody將請求的Http Body和參數messge進行了映射。

運行后,使用curl向服務端提交json數據。提交的請求頭部要帶上"Content-Type:application/json",表明請求體Json。

?
1
curl -i -H "Content-Type:application/json" -d "{\"from\":\"Tom\",\"to\":\"Sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1

結果:

?
1
{"id":9,"content":"Tom speak to Sandy 'hello buddy'"}

4.Post請求,參數以Http body的途徑提交表單數據。

在EchoController增加響應的方法,并完成映射。

?
1
2
3
4
@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方法的參數中用@ModelAttribute將請求的Http Body中的表單數據和參數messge進行了映射。

運行后,使用curl向服務端提交表單數據。提交的請求頭部要帶上"Content-Type:application/x-www-form-urlencoded",表明請求體是表單數據,格式是"key1=value1&key2=value2&key3=value3"。

?
1
curl -i -H "Content-Type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2

 結果:

?
1
{"id":11,"content":"sandy speak to aissen 'go to'"}

總結

以上所述是小編給大家介紹的SpringBoot構建Restful service完成Get和Post請求,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.cnblogs.com/kongxianghai/p/7366535.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 福利片中文 | 故意短裙公车被强好爽在线播放 | 国产精品久久久久久网站 | 好吊色青青青国产综合在线观看 | 日韩成人一级 | 日本不卡在线视频高清免费 | 国产欧美又粗又猛又爽老 | 日韩爱爱| 手机在线观看网站免费视频 | 午夜精品久久久久 | 亚洲精品私拍国产福利在线 | 黑人群性xxx | 动漫美女胸被狂揉扒开吃奶动态图 | 欧美日韩国产一区二区三区在线观看 | 亚洲国产成人久久综合一区 | 东京道一本热大交乱 | 精品国产一区二区三区在线 | 精品久久久久久国产91 | 国模孕妇季玥337p人体 | 国产欧美亚洲精品第一页青草 | 国产中文在线视频 | 欧美日韩精品乱国产 | h日本漫画全彩在线观看 | 亚洲热在线视频 | 国自产拍在线天天更新91 | 赤坂丽女医bd无删减在线观看 | 成年人免费看的视频 | 9191久久| 国产一区二区三区福利 | 国产悠悠视频在线播放 | 视频一区二区三区在线 | igao网果冻传媒 | 天堂bt在线| 99热这里只有精品国产免费 | 成人国产在线视频 | 欧美日韩第二页 | 国产成人看片免费视频观看 | 欧美福利在线播放 | 国产成人精品福利色多多 | 日韩精品一区二区三区老鸭窝 | 韩国靠逼 |