常用注解
@PathVariable 路径参数获取信息
<a href="/https/blog.csdn.net/monster/100/king">@PathVariable-路径变量:/monster/100/king</a>
package com.sun.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class ParameterController {
@GetMapping("/monster/{id}/{name}") //接受两个路径参数
public String pathVariable(@PathVariable("id") Integer id, @PathVariable("name") String name,
@PathVariable Map<String, String> map) { //这里的map指将所有的路径参数都放到map中
System.out.println("id:" + id + " name:" + name);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key:" + entry.getKey() + " value: " + entry.getValue());
}
return "success"; //返回json给浏览器
}
}
@RequestHeader 请求头获取信息
package com.sun.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class ParameterController {
@GetMapping("/requestHeader") //获取请求头的信息
public String requestHeader(@RequestHeader("host") String host, @RequestHeader Map<String, String> header) {
Syste