什么是RestFul风格:
一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
历史:
REST 来自于Roy Thomas Fielding 2000年的博士论文 - 《Architectural Styles and the Design of Network-based Software Architectures》
基本概念
Representational State Transfer,is one way of providing interoperability between computer systems on the Internet.
restful风格实现的机制有多种,常见的有RPC和基于web的服务
用java实现RESTFUL
spring良好的支持restful风格,我们用springboot实现rest风格
核心的注释:
@Controller
标识该类是spring的一个bean
@RestController
@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 (or empty String otherwise)
* @since 4.0.1
*/
@AliasFor(annotation = Controller.class)
String value() default "";
}
从源码可以看出,@RestController里面引用了@Controller和@ResponseBody
而@ResponseBody是没有视图渲染,直接将Model经过处理之后返回给前端
@PathVariable
从请求中获取值
package com.tangbaobao.spring_boot_study.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tangxuejun
* @version 2018/7/29 下午12:32
*/
@RestController
public class RestRequest {
@GetMapping("api/getParam/{userName}")
public String getParam(@PathVariable(name = "userName") String userName) {
return "我接到的请求值为:userName:" + userName;
}
}
通过访问localhost:8080/api/getParam/tanxuejun,可以得出以下结果:
Rest返回不同格式的数据
说明,在此例子中用到了的User实体
package com.tangbaobao.spring_boot_study.pojo;
import lombok.Data;
/**
* @author tangxuejun
* @version 2018/7/28 下午10:27
*/
@Data
public class User {
private String userName;
private int age;
}
在webMvcConfigurationSupport这个类中,我们可以看到springMVC提供了很多返回类型,通过引入响应的jar包,以及配置,可返回HTML,xml,json等数据
通过请求返回HTML
package com.tangbaobao.spring_boot_study.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tangxuejun
* @version 2018/7/28 下午10:48
*/
@RestController
@RequestMapping("/api")
public class RespHTML {
@GetMapping("/respHTML")
public String returnHTML() {
String string = "<html><body>hello i am html</body></html>";
return string;
}
}
扫描二维码关注公众号,回复:
2758903 查看本文章
返回json
package com.tangbaobao.spring_boot_study.rest;
import com.tangbaobao.spring_boot_study.pojo.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tangxuejun
* @version 2018/7/28 下午11:03
*/
@RestController
public class RespJSON {
@GetMapping(value = "/api/respJSON",produces = MediaType.APPLICATION_JSON_VALUE)
public User respJSON() {
User user = new User();
user.setAge(11);
user.setUserName("gson");
return user;
}
}
返回XML
我们要引入响应的pom依赖
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
package com.tangbaobao.spring_boot_study.rest;
import com.tangbaobao.spring_boot_study.pojo.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tangxuejun
* @version 2018/7/28 下午10:24
*/
@RestController
public class RespXML {
@GetMapping(value = "/api/respXML",produces = MediaType.APPLICATION_XHTML_XML_VALUE)
public Object respXML() {
User user = new User();
user.setAge(1);
user.setUserName("tangbaobao");
return user;
}
}