@PathVariable注解主要用来获取URL参数。即这种风格的 URL:http://localhost:8080/user/{id}前端
@GetMapping("/user/{id}") public String testPathVariable(@PathVariable Integer id) { System.out.println("获取到的id为:" + id); return "success"; }
对于多个参数的获取spring
@GetMapping("/user/{idd}/{name}") public String testPathVariable( @PathVariable(value = "idd") Integer id, @PathVariable String name) { System.out.println("获取到的id为:" + id); System.out.println("获取到的name为:" + name); return "success"; }
@RequestParam:是从 Request 里获取参数值,即这种风格的 URL:http://localhost:8080/user?id=1。除此以外,该注解还能够用于 POST 请求,接收前端表单提交的参数json
@RequestMapping("/user") public String testRequestParam( @RequestParam(value = "idd", required = false) Integer id) { System.out.println("获取到的id为:" + id); return "success"; }
当参数较多时,能够不用@RequestParam。而是经过封装实体类来接收参数。后端
public class User { private String username; private String password; //添加setter和getter }
使用实体接收的话,咱们没必要在前面加 @RequestParam 注解,直接使用便可。restful
@PostMapping("/form2") public String testForm(User user) { System.out.println("获取到的username为:" + user.getUsername()); System.out.println("获取到的password为:" + user.getPassword()); return "success"; }
上面的是表单实体提交。当JSON格式提交时,须要用@RequestBody。
@RequestBody 注解用于接收前端传来的实体。接收参数为JSON格式的传递。网络
public class User { private String username; private String password; //省略getter和setter } @PostMapping("/user") public String testRequestBody(@RequestBody User user) { System.out.println("获取到的username为:" + user.getUsername()); System.out.println("获取到的password为:" + user.getPassword()); return "success"; }
传输时须要传JSON格式的参数。架构
先后端传参通常使用Restful规范
RESTful 架构一个核心概念是“资源”(Resource)。从 RESTful 的角度看,网络里的任何东西都是资源,能够是一段文本、一张图片、一首歌曲、一种服务等,每一个资源都对应一个特定的 URI(统一资源定位符),并用它进行标示,访问这个 URI 就能够得到这个资源。
spring boot的注解很好的支持了restful格式app
@RestController注解可将返回的数据结果转换成json格式,在sprinboot中默认使用的JSON解析技术框架是Jackson。
基本示例框架
@RestController @RequestMapping("/") public class Hello { @GetMapping(value = "hello") public String hello(){ return "hello world"; } }
在项目中,遇到null值时,但愿把null都转成""。须要设置一个配置ide
@Configuration public class Jackson { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } }
就会自动把null值转换为空值""
在后台返回的接口数据中,通常要求结构是统一的,包括有状态码、返回信息。因此能够用泛型封装一个统一的JSON返回结构
public class JsonResult<T> { private T data; private String code; private String msg; /** * 若没有数据返回,默认状态码为0 */ public JsonResult(T data){ this.data = data; this.code = "10200"; this.msg = "操做成功"; } //省略getter和setter
修改controller中的代码
@RequestMapping("/list") public JsonResult<List> getStudentList(){ List<Student> list = new ArrayList<>(); Student stu1 = new Student(2,"22",null); Student stu2 = new Student(3,"33",null); list.add(stu1); list.add(stu2); return new JsonResult<>(list); }
访问url,返回的格式将是:
{"data": [{"id":2,"username":"22","password":""}, {"id":3,"username":"33","password":""}], "code":"10200", "msg":"操做成功"}
author.name=zhangsan
可使用@Value注解便可获取配置文件中的配置信息
@Value("${author.name}") private String userName;
配置信息以下:
url.orderUrl=http://localhost:8002 url.userUrl=http://localhost:8003 url.shoppingUrl=http://localhost:8004
新建一个配置类,来保存配置
@Component @ConfigurationProperties(prefix = "url") public class MicroServiceUrl { private String orderUrl; private String userUrl; private String shoppingUrl; // 省去 get 和 set 方法 }
@Component 注解是把该类做为组件放在spring容器中,使用时直接注入便可。
@ConfigurationProperties注解就是指明该类中的属性名就是配置中去掉前缀后的名字
使用ConfigurationProperties须要加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
使用Resource注解就能够将添加配置类MicroServiceUrl引入到controller中使用了
@Resource private MicroServiceUrl microServiceUrl; @GetMapping(value = "getResource") public String getR(){ return microServiceUrl.getUserUrl(); }