一、创建文件夹和类
新建一个idea的maven项目,创建相应的文件夹和类,结构如下
二、get请求案例
1.HelloController代码如下
package org.example.test_demo1.cotroller;
import org.springframework.web.bind.annotation.*;
//加上注解表示建立一个控制器,控制器的作用就是接收web的访问请求
@RestController
public class HelloController {
//案例一,访问网址:http://localhost8080:/hello
//下面这个注解表示能够识别web的get请求
//@GetMapping("/hello")此注解等同于下面的注解方式
@RequestMapping(value = "/hello",method = RequestMethod.GET)
//下面的这个函数表示接收get请求之后返回的内容,当函数进行修改时,必须重启项目才能生效
public String Helloword(){
return "hello word11";
}
//例,http://localhost:8080/test1
@RequestMapping("/test1")
public String test1(){
return "你好";
}
//案例二,表示接收前端传来的name参数,访问网址:http://localhost8080:/hello1?name=zhangsan
// 注意这里要传入的名称为name的参数必须在下面的函数中传参hello(String name),命名必须一样
@RequestMapping(value="/hello1",method = RequestMethod.GET)
public String hello(String name){
return "你好"+name;
}
//例,http://localhost:8080/test2?name=ycl
@RequestMapping("/test2")
public String test2(String name){
return name;
}
//案例三,表示接收前端传来的多个参数,访问网址:http://localhost8080:/hello2?name=zhangsan&password=123
// 注意这里要传入的名称为?name=zhangsan&password=123的参数必须在下面的hello函数中传参hello(String name,String password),命名必须一样
@RequestMapping(value="/hello2",method = RequestMethod.GET)
public String hello(String name,String password){
System.out.println("你好,密码为:"+password);
return "你好,名称为:"+name;
}
//例,http://localhost:8080/test3?name=ycl&password=123
@RequestMapping("/test3")
public String test3(String name,String password){
return "你好,名称为:"+name+"密码为:"+password;
}
// 案例四,如果web请求的数据名称和我们定义的函数变量不一致,可以用@RequestParam注解来标明
// required = false表示在前端请求的时候可以不访问这个参数,不会报错;如果不加入这个参数,则不访问hostname将会报错
// http://localhost:8080/test4?hostname=ycl
@RequestMapping("/test4")
public String test4(@RequestParam(value = "hostname",required = false)String name){
return name;
}
// 案例五:通配符
// http://localhost:8080/test666*(*表示任意字符)
@GetMapping("/test666*")
public String test666(){
return "通配符测试";
}
// http://localhost:8080/test667/xxx(test667后面最多只能再加一个文件夹)
@GetMapping("/test667/*")
public String test667(){
return "通配符测试1";
}
// http://localhost:8080/test668/xxx/xxx(test668后面可以添加无数个文件夹)
@GetMapping("/test668/**")
public String test668(){
return "通配符测试2";
}
}
2.运行案例
三、post请求案例
1.建立User实体类
package org.example.test_demo1.entity;
public class User {
// 当web前端向后端传递参数的时候,参数名称必须和下面的两个属性一致
// 例如,必须是localhost:8080/test?username=ycl&password=123
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;
}
}
2.PostController代码如下
package org.example.test_demo1.cotroller;
import org.example.test_demo1.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
//注意:只有Get请求才能通过浏览器进行测试,Post请求在浏览器测试会报错,只能使用Apipost
@RestController
public class PostCotroller {
//localhost:8080/Testpost
@RequestMapping(value ="/Testpost",method = RequestMethod.POST)
public String Testpost(){
return "post请求1";
}
//localhost:8080/Testpost1?name=ycl&sex=women
//或者localhost:8080/Testpost1,参数在body里面传递
@RequestMapping(value ="/Testpost1",method = RequestMethod.POST)
public String Testpost1(String name,String sex){
return name+","+sex;
}
// localhost:8080/Testpost2?username=ycl&password=1234
@RequestMapping(value ="/Testpost2",method = RequestMethod.POST)
public String Testpost2(User user){
System.out.println(user);
return "post请求2";
}
// localhost:8080/Testpost3 web传递json数据类型,要加@RequestBody,传递的名称和数据类型都要和User定义的属性一致
@RequestMapping(value ="/Testpost3",method = RequestMethod.POST)
public String Testpost3(@RequestBody User user){
System.out.println(user);
return "post请求3";
}
}
3.运行实例
注意:Post请求不能在浏览器进行测试!!!我们这里借助软件ApiPost进行测试,下载网址:https://www.apipost.cn/download.html)