日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring RESTFul Client – RestTemplate Example--转载

發(fā)布時間:2025/4/5 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring RESTFul Client – RestTemplate Example--转载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址:http://howtodoinjava.com/2015/02/20/spring-restful-client-resttemplate-example/

After learning to build?Spring REST based RESTFul APIs?for?XML representation?and?JSON representation, let’s build a RESTFul client to consume APIs which we have written. Accessing a third-party REST service inside a Spring application revolves around the use of the Spring?RestTemplate?class. The?RestTemplate?class is designed on the same principles as the many other Spring *Template classes (e.g.,?JdbcTemplate,?JmsTemplate?), providing a simplified approach with default behaviors for performing complex tasks.

Given that the?RestTemplate?class is designed to call REST services, it should come as no surprise that its main methods are closely tied to REST’s underpinnings, which are the HTTP protocol’s methods: HEAD, GET, POST, PUT, DELETE, and OPTIONS. E.g. it’s methods are?headForHeaders(),?getForObject(),?postForObject(),?put()and?delete()?etc.

Read More and Source Code :?Spring REST JSON Example

HTTP GET Method Example

1) Get XML representation of employees collection in String format

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET) public String getAllEmployeesXML(Model model) { ????model.addAttribute("employees", getEmployeesCollection()); ????return "xmlTemplate"; }

REST Client Code

1 2 3 4 5 6 7 8 9 private static void getEmployees() { ????final String uri =?"http://localhost:8080/springrestexample/employees.xml"; ????? ????RestTemplate restTemplate =?new RestTemplate(); ????String result = restTemplate.getForObject(uri, String.class); ????? ????System.out.println(result); }

2) Get JSON representation of employees collection in String format

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees", produces = MediaType.APPLICATION_JSON_VALUE,? method = RequestMethod.GET) public String getAllEmployeesJSON(Model model) { ????model.addAttribute("employees", getEmployeesCollection()); ????return "jsonTemplate"; }

REST Client Code

1 2 3 4 5 6 7 8 9 private static void getEmployees() { ????final String uri =?"http://localhost:8080/springrestexample/employees.json"; ????? ????RestTemplate restTemplate =?new RestTemplate(); ????String result = restTemplate.getForObject(uri, String.class); ????? ????System.out.println(result); }

3) Using custom HTTP Headers with RestTemplate

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees", produces = MediaType.APPLICATION_JSON_VALUE,? method = RequestMethod.GET) public String getAllEmployeesJSON(Model model) { ????model.addAttribute("employees", getEmployeesCollection()); ????return "jsonTemplate"; }

REST Client Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 private static void getEmployees() { ????final String uri =?"http://localhost:8080/springrestexample/employees"; ????? ????RestTemplate restTemplate =?new RestTemplate(); ????? ????HttpHeaders headers =?new HttpHeaders(); ????headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); ????HttpEntity<String> entity =?new HttpEntity<String>("parameters", headers); ????? ????ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class); ????? ????System.out.println(result); }

4) Get data as mapped object

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET) public String getAllEmployeesXML(Model model) { ????model.addAttribute("employees", getEmployeesCollection()); ????return "xmlTemplate"; }

REST Client Code

1 2 3 4 5 6 7 8 9 private static void getEmployees() { ????final String uri =?"http://localhost:8080/springrestexample/employees"; ????RestTemplate restTemplate =?new RestTemplate(); ????? ????EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class); ????? ????System.out.println(result); }

5) Passing parameters in URL

REST API Code

1 2 3 4 5 6 7 8 9 @RequestMapping(value =?"/employees/{id}") public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id")?int id) { ????if (id <=?3) { ????????EmployeeVO employee =?new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com"); ????????return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK); ????} ????return new ResponseEntity(HttpStatus.NOT_FOUND); }

REST Client Code

1 2 3 4 5 6 7 8 9 10 11 12 private static void getEmployeeById() { ????final String uri =?"http://localhost:8080/springrestexample/employees/{id}"; ????? ????Map<String, String> params =?new HashMap<String, String>(); ????params.put("id",?"1"); ????? ????RestTemplate restTemplate =?new RestTemplate(); ????EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params); ????? ????System.out.println(result); }

HTTP POST Method Example

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees", method = RequestMethod.POST) public ResponseEntity<String> createEmployee(@RequestBody EmployeeVO employee) { ????System.out.println(employee); ????return new ResponseEntity(HttpStatus.CREATED); }

REST Client Code

1 2 3 4 5 6 7 8 9 10 11 private static void createEmployee() { ????final String uri =?"http://localhost:8080/springrestexample/employees"; ????EmployeeVO newEmployee =?new EmployeeVO(-1,?"Adam",?"Gilly",?"test@email.com"); ????RestTemplate restTemplate =?new RestTemplate(); ????EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class); ????System.out.println(result); }

HTTP PUT Method Example

REST API Code

1 2 3 4 5 6 7 @RequestMapping(value =?"/employees/{id}", method = RequestMethod.PUT) public ResponseEntity<EmployeeVO> updateEmployee(@PathVariable("id")?int id,?@RequestBody EmployeeVO employee) { ????System.out.println(id); ????System.out.println(employee); ????return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK); }

REST Client Code

1 2 3 4 5 6 7 8 9 10 11 12 private static void deleteEmployee() { ????final String uri =?"http://localhost:8080/springrestexample/employees/{id}"; ????? ????Map<String, String> params =?new HashMap<String, String>(); ????params.put("id",?"2"); ????? ????EmployeeVO updatedEmployee =?new EmployeeVO(2,?"New Name",?"Gilly",?"test@email.com"); ????? ????RestTemplate restTemplate =?new RestTemplate(); ????restTemplate.put ( uri, updatedEmployee, params); }

HTTP DELETE Method Example

REST API Code

1 2 3 4 5 6 @RequestMapping(value =?"/employees/{id}", method = RequestMethod.DELETE) public ResponseEntity<String> updateEmployee(@PathVariable("id")?int id) { ????System.out.println(id); ????return new ResponseEntity(HttpStatus.OK); }

REST Client Code

1 2 3 4 5 6 7 8 9 10 private static void deleteEmployee() { ????final String uri =?"http://localhost:8080/springrestexample/employees/{id}"; ????? ????Map<String, String> params =?new HashMap<String, String>(); ????params.put("id",?"2"); ????? ????RestTemplate restTemplate =?new RestTemplate(); ????restTemplate.delete ( uri,? params ); }

Let me know if something needs more explanation.

Happy Learning !!

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4552018.html

總結(jié)

以上是生活随笔為你收集整理的Spring RESTFul Client – RestTemplate Example--转载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 岛国视频一区 | 可以免费看的黄色网址 | 爽妇网国产精品 | 麻豆国产91在线播放 | 久久在线免费 | 最近免费中文字幕中文高清百度 | av大片免费看 | 午夜精品久久久内射近拍高清 | 黄网免费视频 | 国产色综合天天综合网 | 女人裸体无遮挡 | 欧美日韩一区二区三区不卡视频 | 99欧美| 国产精品成av人在线视午夜片 | 午夜精品久久久 | 中国在线观看免费高清视频播放 | 日本久久亚洲 | 白丝美女被草 | 国产一区激情 | 国内少妇精品 | 欧美电影一区二区三区 | 日日夜夜网站 | 在线免费观看污视频 | 国产精品七区 | 国产精品污污 | 日韩免费影视 | 亚洲av无一区二区三区 | 欧美一级看片 | 三女警花合力承欢猎艳都市h | 欧美三级视频 | 四虎影院永久 | 成人在线观看视频网站 | 精品白浆| 国产福利一区二区三区 | 图片一区二区 | 精品无码一区二区三区免费 | 久久在线播放 | 男女一区二区三区 | 婷婷激情综合 | 日本高清视频一区二区 | 一本一本久久a久久精品综合麻豆 | 福利视频一二区 | 四虎av影视| 性高潮久久久久久久久 | 青青av| 日b视频免费看 | 男人懂得网站 | 天海翼一区二区 | 在线精品自拍 | 26uuu亚洲国产精品 | 成人高潮片免费 | 成人免费区一区二区三区 | 欧美韩一区二区 | 风韵多水的老熟妇 | 男生吃小头头的视频 | 老司机午夜免费视频 | 麻豆性生活 | 久久久久少妇 | 自拍偷拍五月天 | 国产精品永久久久久久久久久 | 一个色的综合 | 成人免费视频网站在线观看 | 午夜三级网站 | 日本国产中文字幕 | 樱花草涩涩www在线播放 | 国产免费黄色大片 | 成人在线中文字幕 | 中文第一页 | 国产精品9191 | 在线观看免费视频 | 日韩影院一区二区 | 91av在| 日韩欧美亚洲一区 | 卡一卡二视频 | 夜夜嗨av色一区二区不卡 | 一本色道久久亚洲综合精品蜜桃 | 999精品 | 日韩欧美国产成人精品免费 | 国产精品1000部啪视频 | 白嫩日本少妇做爰 | 超碰在线网址 | 人人爽人人爽人人爽 | 精品乱| 精品99久久久久成人网站免费 | 成人动漫免费在线观看 | 性网站在线观看 | 污污视频在线播放 | 欧美日韩观看 | 国产美女91 | av精选 | 日本欧美韩国国产精品 | 国产乱强伦一区二区三区 | 国产女主播一区二区三区 | 老妇裸体性激交老太视频 | 日本精品一区视频 | 香蕉视频亚洲一级 | 亚洲国产一区二区三区a毛片 | 黄色三级网站 | 日韩v片 |