一文帶你搞懂Java中Get和Post的使用
1 Get請(qǐng)求數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-get-post
1.1 Controller
文件名MyController,內(nèi)容為:
@RestController @RequestMapping("/homepage") publicclass MyController { @Autowired MyService myService; @GetMapping("/learnGet") public String learnGet(){ return myService.learnGet(); } }
1.2 Service
文件名MyService,內(nèi)容為:
@Service @EnableScheduling publicclass MyService { public String learnGet(){ Long timeLong = System.currentTimeMillis(); SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //設(shè)置格式 String timeString = timeFormat.format(timeLong); return timeString; } }
1.3 Application
在application.properties配置:
# 設(shè)置端口號(hào) server.port=8888
1.4 Postman
配置Get,地址為:http://localhost:8888/homepage/returnTime 。
即可獲得當(dāng)前時(shí)間戳。
2 Post接收數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-get-post
2.1 Controller
文件名MyController,內(nèi)容為:
@RestController @RequestMapping("/homepage") publicclass MyController { @Autowired MyService myService; @PostMapping("/postReceive") public Map<String, Object> postReceive(@RequestParam("number") int number, @RequestParam("name") String name) { return myService.postReceive(number, name); } @PostMapping("/postReceiveByMap") public Map<String, Object> postReceiveByMap(@RequestParam Map<String, Object> map) { System.out.println("map:" + map + "\n"); return myService.postReceiveByMap(map); } }
2.2 Service
文件名MyService,內(nèi)容為:
@Service @EnableScheduling publicclass MyService { public Map<String, Object> postReceive(int number, String name){ Map<String, Object> res = new HashMap<>(); res.put("number", number); res.put("name", name); return res; } public Map<String, Object> postReceiveByMap(Map<String, Object> map){ int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number")); String name = map.get("name") == null ? "" : (String)map.get("name"); Map<String, Object> res = new HashMap<>(); res.put("number", number); res.put("name", name); System.out.println("map:" + map + "\n"); System.out.println("res:" + res + "\n"); return res; }
2.3 Application
在application.properties配置:
# 設(shè)置端口號(hào) server.port=8888
2.4 Postman
配置Get,地址為:http://localhost:8888/homepage/returnTime 。
即可獲得輸出。
3 Post發(fā)送數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-post-send
需要注意,RestTemplate在postForObject時(shí),用MultiValueMap,不可使用HashMap。
3.1 Controller
文件名MyController,內(nèi)容為:
@RestController @RequestMapping("/homepage") publicclass MyController { @Autowired MyService myService; @PostMapping("/postSend") public Map<String, Object> postSend() { return myService.postSend(); } }
3.2 Service
文件名MyService,內(nèi)容為:
@Service @EnableScheduling publicclass MyService { @Resource private RestTemplate restTemplate; String URL = "http://localhost:8888/homepage/postReceiveByMap"; public Map<String, Object> postSend(){ Map<String, Object> sendData = new HashMap<>(); sendData.put("number", 3); sendData.put("name", "張三"); ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(URL, sendData, ResponseResult.class); Map<String, Object> returnData = new HashMap<>(); returnData.put("StatusCode:", responseData.getStatusCode()); returnData.put("Body:", responseData.getBody()); return returnData; } }
3.3 ResponseResult
publicclass ResponseResult { privateint number; private String name; public ResponseResult(){ } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return"ResponseResult [number=" + number + ",name=" + name + "]"; } }
3.4 Config
@Configuration publicclass Config { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder){ return builder.build(); } }
3.5 Application
在application.properties配置:
# 設(shè)置端口號(hào) server.port=8889
3.6 Postman
配置Post,地址為: http://localhost:8889/homepage/postSend
即可獲得輸出。
以上就是一文帶你搞懂Java中Get和Post的使用的詳細(xì)內(nèi)容,更多關(guān)于Java Get Post的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring cloud gateway集成hystrix實(shí)戰(zhàn)篇
這篇文章主要介紹了spring cloud gateway集成hystrix實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07java 父類子類有同名方法時(shí)如何調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了java 父類子類有同名方法時(shí)如何調(diào)用的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Spring?Boot用戶注冊(cè)驗(yàn)證的實(shí)現(xiàn)全過程記錄
最近在設(shè)計(jì)自己的博客系統(tǒng),涉及到用戶注冊(cè)與登錄驗(yàn)證,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot用戶注冊(cè)驗(yàn)證的實(shí)現(xiàn)全過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息
這篇文章主要介紹了SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類,涉及java針對(duì)鍵盤監(jiān)聽及數(shù)字運(yùn)算的處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(二)之MyBatis原始Dao開發(fā)和mapper代理開發(fā)
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(二)之MyBatis原始Dao開發(fā)和mapper代理開發(fā),需要的朋友可以參考下2017-05-05Elasticsearch term 查詢之精確值搜索功能實(shí)現(xiàn)
term查詢是Elasticsearch中用于精確值搜索的一種基本方式,通過了解 term 查詢的工作原理和使用方法,你可以更好地利用 Elasticsearch 進(jìn)行結(jié)構(gòu)化數(shù)據(jù)的搜索和分析,本文將詳細(xì)介紹 term 查詢的工作原理、使用場(chǎng)景以及如何在 Elasticsearch 中應(yīng)用它,感興趣的朋友一起看看吧2024-06-06