博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第 7 章 RestTemplate - Spring4 Restful
阅读量:6452 次
发布时间:2019-06-23

本文共 5434 字,大约阅读时间需要 18 分钟。

首先我要禁告各位,Spring发展过程中,每个版本都有一定差异。如果你做实验失败后在网上搜索答案,切记看一下版本号还有文章帖子的发布时间。否则你可能按照Spring3配置方法去Spring4。

@RestController 默认返回 @ResponseBody, 所以@ResponseBody可加可不加

7.1. RestTemplate Example

7.1.1. pom.xml

Maven 增加 jackson 开发包

com.fasterxml.jackson.dataformat
jackson-dataformat-xml
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.core
jackson-annotations

7.1.2. web.xml

url-pattern匹配中增加*.xml跟*.json

springframework
org.springframework.web.servlet.DispatcherServlet
1
springframework
/welcome.jsp
/welcome.html
*.json
*.xml
*.html

7.1.3. springframework.xml

7.1.4. RestController

package cn.netkiller.controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestController;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.ResponseBody;import cn.netkiller.pojo.Message;@RestController@RequestMapping("/rest")public class TestRestController {	public TrackerRestController() {		// TODO Auto-generated constructor stub	}	@RequestMapping("welcome")	@ResponseStatus(HttpStatus.OK)	public String welcome() {		return "Welcome to RestTemplate Example.";	}	@RequestMapping(value = "test", method = RequestMethod.GET, produces = { "application/xml", "application/json" })	@ResponseStatus(HttpStatus.OK)	public @ResponseBody Message test(@RequestHeader(value = "accept") String accept) {		Message message = new Message();		message.setTitle("test");		message.setText("Helloworld!!!");		System.out.println("accept: " + accept);		System.out.println(message.toString());		return message;	}	@RequestMapping("message/{name}")	public ResponseEntity
message(@PathVariable String name) { Message msg = new Message(); msg.setTitle(name); return new ResponseEntity
(msg, HttpStatus.OK); } @RequestMapping(value = "create", method = RequestMethod.POST, produces = { "application/xml", "application/json" }) public ResponseEntity
create(@RequestBody Tracker tracker) { this.mongoTemplate.insert(tracker); return new ResponseEntity
(tracker, HttpStatus.OK); } @RequestMapping(value = "read", method = RequestMethod.GET, produces = { "application/xml", "application/json" }) @ResponseStatus(HttpStatus.OK) public ArrayList
read() { ArrayList
trackers = (ArrayList
) mongoTemplate.findAll(Tracker.class); return trackers; } }

7.1.5. POJO

package cn.netkiller.pojo;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Message {		String title;    String text;    	public Message() {		// TODO Auto-generated constructor stub	}		//@XmlElement	@XmlAttribute  	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}		//@XmlElement	@XmlAttribute	public String getText() {		return text;	}	public void setText(String text) {		this.text = text;	}	@Override	public String toString() {		return "Message [title=" + title + ", text=" + text + "]";	}}

7.1.6. 测试

neo@netkiller:~/www.netkiller.cn$ curl http://172.16.0.1:8080/spring4/rest/welcome.htmlWelcome to RestTemplate Example.neo@netkiller:~/www.netkiller.cn$ curl http://172.16.0.1:8080/spring4/rest/test.json{"title":"test","text":"Helloworld!!!"}neo@netkiller:~/www.netkiller.cn$ curl http://172.16.0.1:8080/spring4/rest/test.xml
test
Helloworld!!!
neo@netkiller:~/www.netkiller.cn$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"login":"neo", "unique":"356770257607079474","hostname":"www.example.com","referrer":"http://www.netkiller.cn","href":"http://www.netkiller.cn"}' http://172.16.0.1:8080/spring4/rest/create.jsonHTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: application/jsonTransfer-Encoding: chunkedDate: Tue, 21 Jun 2016 03:08:26 GMT{"name":"neo","unique":"356770257607079474","hostname":"www.netkiller.cn","referrer":"http://www.netkiller.cn","href":"http://www.netkiller.cn"}

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
os.walk函数
查看>>
[Unity3d]DrawCall优化手记
查看>>
细数.NET 中那些ORM框架 —— 谈谈这些天的收获之一
查看>>
SQL Serever学习7——数据表2
查看>>
洛谷——P2404 自然数的拆分问题
查看>>
(转)Mac 下设置android NDK的环境
查看>>
[struts]s:action 的使用方法
查看>>
dubbo问题总结
查看>>
20165320 第三周学习总结
查看>>
Struts2和Spring MVC的区别
查看>>
angular-bootstrap ui-date组件问题总结
查看>>
理解Javascript参数中的arguments对象
查看>>
day1
查看>>
(salesforce相关)AngularJs实现表格的增删改查
查看>>
p2:千行代码入门python
查看>>
bzoj1106[POI2007]立方体大作战tet*
查看>>
解决:Java调用.net的webService产生“服务器未能识别 HTTP 标头 SOAPAction 的值”错误...
查看>>
spring boot configuration annotation processor not found in classpath问题解决
查看>>
【转】正则基础之——神奇的转义
查看>>
团队项目测试报告与用户反馈
查看>>