SpringMVC响应

准备好App:Apipost

编写测试类

1.Get请求:

java 复制代码
package com.qcby.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/role")//一级目录
public class RoleController {
    //二级目录
    @RequestMapping(path = "/list", method = RequestMethod.GET)//默认为get请求
    @ResponseBody//告诉springmvc这是一个返回值  没有这个注解就要有对应的页面!!!
    public String list(){
        return "hello save";
    }
}

1.1 get请求发送数据

java 复制代码
@RequestMapping(path = "/save", method = RequestMethod.GET)
@ResponseBody
public String save(String name){
    System.out.println(name);
    return "hello save";
}

1.2 多参数之间拼接就可以了

1.3 非常多的参数

创建一个model层

在model层里面创建类在类里面定义参数和Getter、Setter方法

这里可以下pom文件中导入Lombok依赖使用@Data和@ToString注解方

java 复制代码
package com.qcby.model;
public class User {
private Integer id;
private String username;
private String password;
private String address;
@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", address='" + address + '\'' +
            '}';
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

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;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

}
java 复制代码
@RequestMapping(path = "/save2", method = RequestMethod.GET)
@ResponseBody
public String save2(User user){
    System.out.println(user);
    return "hello save";
}

2. post传值

2.1 使用Body里面的form-data,只有几个数据

java 复制代码
@RequestMapping(path = "/save3", method = RequestMethod.POST)
@ResponseBody
public String save3(String name,Integer age){
    System.out.println(name+" "+ age);
    return "hello save";
}

2.2 有默认值

required = false表示没有必要必须填值

java 复制代码
//有默认值
@RequestMapping(path = "/save5", method = {RequestMethod.GET})
@ResponseBody
public String save6(@RequestParam(name = "username", required = false,defaultValue = "qcbby") String name,
                    @RequestParam(name = "age", required = false,defaultValue = "18") Integer age){
    System.out.println( name + " " + age );
    return "hello save";
}

2.3 Restful风格

java 复制代码
@GetMapping("/save6/{id}")//Restful风格
@ResponseBody
public String save6(@PathVariable Integer id){
    return "hello save" + id;
}

输入的时候这个id要在网址上写

2.4 使用ServletAPI

java 复制代码
//使用ServletAPI
@RequestMapping("/list")
@ResponseBody
public String list(HttpServletRequest request, HttpServletResponse  response) throws IOException {
    String name = request.getParameter("name");
    response.getWriter().write("hello list"+name);
    return "hello list";
}

返回的是json数据

java 复制代码
@RequestMapping("/save1")
@ResponseBody
public User save1(User user){
    return user;
}
相关推荐
m0_73643930几秒前
JavaScript中显式创建包装对象的后果与性能损耗
jvm·数据库·python
Mr_sst3 分钟前
文件上传并发控制:为什么选Redisson可过期信号量?(避坑指南)
网络·数据库·redis·分布式·安全架构
四维迁跃4 分钟前
JavaScript中Object-defineProperties批量设置属性
jvm·数据库·python
qq_283720055 分钟前
Python3 模块精讲:psycopg2(第三方)- 连接 PostgreSQL
数据库·postgresql
倚楼盼风雨6 分钟前
Redis 为什么快
数据库·redis·缓存
2501_901200536 分钟前
CSS如何让响应式字体在断点处平滑切换_使用clamp函数计算
jvm·数据库·python
xiaoliuliu123458 分钟前
redis-windows-7.2.3安装步骤详解(附Redis配置与Windows服务注册)
数据库·windows·redis
实心儿儿9 分钟前
Linux —— 文件系统_路径解析_软硬连接
linux·运维·服务器
weisian15111 分钟前
Java并发编程--48-美团Leaf与百度UidGenerator:分布式ID生成器的工业级实践
java·leaf号段模式·leaf雪花模式·uidgenerator
dFObBIMmai11 分钟前
如何应对高级SQL注入_配置数据库审计实时监控流量
jvm·数据库·python