SpringBoot请求
常用注解及作用范围:
- @Controller :【类】需要返回一个视图(themleaf),加注解4
@ResponseBody
等于注解2 - @RestController:【类】返回字符串等,与注解1都属于控制器,
- @RequestMapping:【方法/类】设置方法或者类的请求地址,
- @ResponseBody:【方法】支持将返回值放在response体内,而不是返回一个页面,
- @RequestParam:【形参】获取request请求的参数值,
- @RequestBody:【形参】用于将前台发送过来固定格式的数据【xml格式 或者 json等】封装为对应的 JavaBean 对象
- @PathVariable:【形参】用来获得请求url中的动态参数
相关实例及请求
{{local}}:http://localhost:8080
请使用PostMan
请求,无效果请重新构建、刷新缓存等操作。
GET无参请求
请求地址:
{{local}}/hello
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@GetMapping(value = "/hello")
public ResponseEntity<String> hello(){
return new ResponseEntity<>("hello world!", HttpStatus.OK);
}
}
GET单参请求-@PathVariable
请求地址:
{{local}}/user/22
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/user/{userId}",method = RequestMethod.GET)
public ResponseEntity<String> getLogin(@PathVariable("userId") String userId){
System.out.println("User Id : " + userId);
return new ResponseEntity<>(userId, HttpStatus.OK);
}
}
GET多参请求-@RequestParam
请求地址:
{{local}}/user?username=xbhog&pass=111111
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
public ResponseEntity<String> getLogin2(@RequestParam("username") String username,
@RequestParam("pass") String pass){
System.out.println("User name : " + username);
System.out.println("User pass : " + pass);
return new ResponseEntity<>(username, HttpStatus.OK);
}
}
GET Map请求-@RequestParam
请求地址:
{{local}}/userMap?name=xbhog&pass=111111
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/userMap",method = RequestMethod.GET)
public ResponseEntity<String> getLogin3(@RequestParam Map<String,String> user){
System.out.println("User name : " + user.get("name"));
System.out.println("User pass : " + user.get("pass"));
return new ResponseEntity<>(JSON.toJSONString(user), HttpStatus.OK);
}
}
GET对象请求
请求地址:
{{local}}/userPojo?name=xbhog&pass=111111
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/userPojo",method = RequestMethod.GET)
public ResponseEntity<String> getLogin7(User user){
System.out.println("User name : " + user.getName());
System.out.println("User pass : " + user.getPass());
return new ResponseEntity<>(JSON.toJSONString(user), HttpStatus.OK);
}
}
POST单参请求-@RequestBody
请求地址:
{{local}}/user?username=xbhog
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/user",method = RequestMethod.POST)
public ResponseEntity<String> getLogin4(@RequestBody String username){
System.out.println("User name : " + username);
return new ResponseEntity<>(username, HttpStatus.OK);
}
}
POST Map请求-@RequestParam
请求地址:
{{local}}/userMap
------json
{ "name":"xbhog", "pass":"111111" }
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/userMap",method = RequestMethod.GET)
public ResponseEntity<String> getLogin3(@RequestParam Map<String,String> user){
System.out.println("User name : " + user.get("name"));
System.out.println("User pass : " + user.get("pass"));
return new ResponseEntity<>(JSON.toJSONString(user), HttpStatus.OK);
}
}
POST对象请求-@RequestBody
请求地址:
{{local}}/userPojo
-------json
{ "name":"xbhog", "pass":"111111" }
代码如下:
/**
* @author xbhog
*/
@Slf4j
@SpringBootApplication
@RestController
public class AnnotationScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationScaffoldApplication.class, args);
}
@RequestMapping(value="/userPojo",method = RequestMethod.POST)
public ResponseEntity<String> getLogin6(@RequestBody User user){
System.out.println("User name : " + user.getName());
System.out.println("User pass : " + user.getPass());
return new ResponseEntity<>(JSON.toJSONString(user), HttpStatus.OK);
}
}
实现对象
代码如下:
static class User{
private String name;
private String pass;
public User() {
}
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", pass='" + pass + '\'' +
'}';
}
}
Github完整代码:https://github.com/xbhog/AnnotationScaffold
标签:SpringBoot,请求,User,String,user,pass,注解,public,name From: https://www.cnblogs.com/xbhog/p/17279647.html