转发:https://www.iteye.com/blog/wiselyman-2214290

3.1 REST

  • REST:Representational State Transfer;
  • REST是一种数据导向web service,相对于SOAP是一种操作操作和处理导向的web service;
  • Spring为对REST的支持提供了@RestController;
    • 在没有@RestController可以通过@Controller,@ResponseBody组合实现REST控制器;
    • 但是我们经常会使用@ResponseBody这样很麻烦,且易忘记;
    • 使用@RestController替代@Controller,我们就不用使用@ResponseBody;
  • REST支持的http method(通过@RequestMapping的method属性控制)
    • POST:新增
    • GET:读取
    • PUT/PATCH:更新
    • DELETE:删除
  • produces的内容是指定返回的媒体类型让浏览器识别

    • 如返回text/plain的话,浏览器上的js回掉拿到的是字符串,需要自己转换对象;
    • 如返回application/json的话,浏览器上的js拿到的就是js对象而不是字符串,就不需要进行转换;
  • 本例演示向控制器提交json数据,返回结果分别为json和xml格式;

3.2 示例

3.2.1 @RestController源码

从@RestController看出,@RestController是一个元注解,组合了@Controller,@ResponseBody,相当于同时使用了@Controller @ResponseBody

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { /**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
* @since 4.0.1
*/
String value() default ""; }

3.2.2 代码

  • 添加jackson依赖

jackson-dataformat-xml依赖jackson-bind,这样我们能同时返回xml和json

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.5.3</version>
</dependency>

若只需返回json数据(大多数项目都是这样),将上面依赖更换为

 <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
  • 传值对象
package com.wisely.web;

public class DemoObj {
private Long id;
private String name;
//此处一定要有空构造,不然会有400 bad request,主要是jackson将json参数转换为对象需要
public DemoObj() {
super();
}
public DemoObj(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
  • DemoMVCConfig注册静态资源
    // 静态资源映射
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
  • 控制器
package com.wisely.web;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api")
public class RESTController {
@RequestMapping(value = "/getjson",produces={"application/json;charset=UTF-8"})
public DemoObj getjson(@RequestBody DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");
}
@RequestMapping(value = "/getxml",produces={"application/xml;charset=UTF-8"})
public DemoObj getxml(@RequestBody DemoObj obj){
return new DemoObj(obj.getId()+1, obj.getName()+"yy");
} }
  • 页面代码
<script type="text/javascript" src="<c:url value="/js/jquery.js" />"></script>
<script type="text/javascript">
var json = {"id":456,"name":"phy"}; $.ajax({
url: "api/getjson",
data: JSON.stringify(json),
type:"POST",
contentType:"application/json",
success: function(data){
console.log(data);
}
});
$.ajax({
url: "api/getxml",
data: JSON.stringify(json),
type:"POST",
contentType:"application/json",
success: function(data){
console.log(data);
}
});
</script>
  • 结果

最新文章

  1. git push error: A Contributor Agreement must be completed before uploading
  2. sqlmap注入检测经验0x01
  3. Semantic-UI和其他几个前端框架
  4. Animating Views Using Scenes and Transitions
  5. Cheatsheet: 2015 04.01 ~ 04.30
  6. Android界面布局学习总结
  7. iOS面试小题集锦
  8. JQUERY省、市、县城市联动选择
  9. C#备份及还原数据库的实现
  10. mac电脑 上强大的RAW图像处理工具 ——RAW Power
  11. JXOI 2017 简要题解
  12. LeetCode contest-95[876,877,&#128065;878]
  13. 自动化运维python学习笔记一
  14. spring学习总结(一)_Ioc基础(上)
  15. msf客户端渗透(四):关闭UAC,hash、防火墙、服务、磁盘、DEP、防病毒、远程桌面、桌面截图、获取tooken
  16. [python][easygui]使用enterbox()方法,简体字会显示成繁体字了
  17. 基于jQuery点击圆形边框弹出图片信息
  18. UML总结--总体架构
  19. Vue中$refs的用法
  20. await Task传异步Lambda问题

热门文章

  1. 洛谷 P3375 【模板】KMP字符串匹配 题解
  2. Ubuntu下彻底卸载默认安装的mysql,自己手动下载安装MYSQL
  3. [TJOI2019]甲苯先生和大中锋的字符串
  4. linux (core dump)调试
  5. 行列式的组合定义及其应用--反对称阵的Pfaffian
  6. (2)React的开发
  7. WARNING: You are using pip version 19.1.1, however version 19.2.1 is available. You should consider upgrading via the &#39;pip install --upgrade pip&#39; command.
  8. 「ZJOI2019」线段树
  9. [ZJOI2007][BZOJ1060]时态同步
  10. 【洛谷】P3188 [HNOI2007]梦幻岛宝珠