博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之rest full 服务搭建
阅读量:4112 次
发布时间:2019-05-25

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

rest full api使用方便能够很好的弱耦合,那么spring对rest full服务很好的支持。

依赖的jar:

org.springframework
spring-web
4.1.0.RELEASE
org.springframework
spring-webmvc
4.1.0.RELEASE

web.xml配置:

spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
spring
/
contextConfigLocation
classpath:spring-admin.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener

spring-mvc.xml:

rest api相关的控制层代码:

@RestController  @RequestMapping("/api")public class ApiController extends BaseController {	private Logger log = LoggerFactory.getLogger(ApiController.class);	@RequestMapping(value = "/{id}", method = RequestMethod.GET)	@ResponseBody	public Item get(@PathVariable String id, HttpServletRequest request) {		Item item = new Item();		item.setId(id);		item.setName("name");		return item;	}	@RequestMapping(method = RequestMethod.POST)	@ResponseBody	public void post(Item item, HttpServletRequest request) {		log.info("post item:{}", item.toString());	}	@RequestMapping(method = RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)	@ResponseBody	public void put(@RequestBody Item item, HttpServletRequest request) {		log.info("put item:{}", item.toString());	}	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)	@ResponseBody	public void delete(@PathVariable String id, HttpServletRequest request) {		log.info("delete id:{}", id);	}}

 

转载地址:http://xoqsi.baihongyu.com/

你可能感兴趣的文章
Subsets 深搜
查看>>
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
谷歌阅读器将于2013年7月1日停止服务,博客订阅转移到邮箱
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>