SpringBoot当中Controller层简单案例介绍

本文详细介绍了在SpringMVC框架下创建Maven项目,构建文件夹结构,以及如何处理GET和POST请求的案例,包括HelloController和PostController的代码实现,以及User实体类的使用。同时提到GET请求可以通过浏览器测试,而POST请求需用APIPost工具进行测试。
摘要由CSDN通过智能技术生成

一、创建文件夹和类

新建一个idea的maven项目,创建相应的文件夹和类,结构如下
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、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

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值