SpringBoot基础入门

资料


路由映射


src/main/java/com/example/helloworld/controller/HelloController.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.helloworld.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

// http://localhost:8080/hello
// http://localhost:8080/hello?nickname=zhangsan&phone=13812345678
@RequestMapping(value = "/hello", method = RequestMethod.GET) //这样就只能接收get请求
// 上面这行等价于@GetMapping("hello")
public String hello(String nickname, String phone) { // 获取到传递的参数
System.out.println(phone);
return "你好" + nickname + "\n你的手机号为" + phone;
}
}



参数传递和数据响应


src/main/java/com/example/helloworld/controller/ParamsController.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

package com.example.helloworld.controller;

import com.example.helloworld.entity.User;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;

@RestController
public class ParamsController {

@RequestMapping(value = "/getTest1", method = RequestMethod.GET) // 必须是get请求才会响应
public String getTest1() {
return "爽哥提示:这是一个GET请求";
}

@RequestMapping(value = "/getTest2", method = RequestMethod.GET)
// http://localhost:8080/getTest2?nickname=xxx&phone=xxx
public String getTest2(String nickname, String phone) {
System.out.println("nickname:" + nickname);
System.out.println("phone:" + phone);
return "GET请求,参数nickname为:" + nickname;
}

@RequestMapping(value = "/getTest3", method = RequestMethod.GET)
// http://localhost:8080/getTest3?nickname=xxx
// 如果前端传递的参数名称,和我方法里定义的变量名称对应不上,可以通过使用@RequestParam注解的方式做一下映射
// 加了这个映射,就要求nickname这个参数就必须传递,否则报400;可以加上required = false解决
public String getTest3(@RequestParam(value = "nickname", required = false) String name) {
System.out.println("nickname:" + name);
return "GET请求,参数name为:" + name;
}


// localhost:8080/postTest1
@RequestMapping(value = "/postTest1", method = RequestMethod.POST)
public String postTest1() {
return "爽哥提示: 这是一个POST请求";
}

@RequestMapping(value = "/postTest2", method = RequestMethod.POST)
public String postTest2(String username, String password) {
// 接收Body form-data格式的数据;或者是Query Params格式(即像get一样?放在路径后面)
System.out.println("username:" + username);
System.out.println("password:" + password);
return "爽哥提示: 这是一个POST请求,username为:" + username + ", password为:" + password;
}

@RequestMapping(value = "/postTest3", method = RequestMethod.POST)
public String postTest3(User user) { // 在entity定义一个User class
// 和上方的case2一样,接收Body form-data格式的数据;或者是Query Params格式(即像get一样?放在路径后面)
System.out.println(user);
return "爽哥提示: 这是一个POST请求,username为:" + user.getUsername() + ", password为:" + user.getPassword();
}

@RequestMapping(value = "/postTest4", method = RequestMethod.POST)
public String postTest4(@RequestBody User user) { // 接收JSON类型,需要加@RequestBody注解
System.out.println(user);
return "爽哥提示: 这是一个POST请求,username为:" + user.getUsername() + ", password为:" + user.getPassword();
}

@GetMapping("/test/*")
public String test() {
return "通配符请求";
}
}


src/main/java/com/example/helloworld/entity/User.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.helloworld.entity;

public class User {
private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}