当前位置: 首页 > article >正文

SpringMVC8-HttpMessageConverter

目录

@RequestBody

RequestEntity

@ResponseBody

SpringMVC处理json

SpringMVC处理ajax

@RestController注解

ResponseEntity


HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象或将Java对象转换为响应报文

HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity

模块springMVC-demo4

@RequestBody

@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当前请求的请求体就会为当前注解所标识的形参赋值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/testRequestBody}" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="测试@RequestBody">
</form>
</body>
</html>
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HttpController {

    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody){
        System.out.println("requestBody:" + requestBody);
        return "success";
    }
}

 

RequestEntity

RequestEntity封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过getHeaders()获取请求头信息,通过getBody()获取请求体信息

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/testRequestEntity}" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="测试RequestEntity">
</form>
</body>
</html>
package com.qcby.mvc.controller;

import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HttpController {


    @RequestMapping("/testRequestEntity")
    public String testRequestEntity(RequestEntity<String> requestEntity){
        //当前requestEntity表示整个请求报文的信息
        System.out.println("请求头:"+requestEntity.getHeaders());
        System.out.println("请求体:"+requestEntity.getBody());
        return "success";
    }
}

 

 

@ResponseBody

@ResponseBody用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testResponseBody}">通过@ResponseBody响应浏览器数据</a>
</body>
</html>
package com.qcby.mvc.controller;

import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HttpController {

    @RequestMapping("/testResponseBody")
    @ResponseBody //返回的success不再是视图,而是响应报文,直接出现在浏览器上
    public String testResponseBody(){
        return "success11111";
    }

}

 

SpringMVC处理json

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.1</version>
</dependency>

在SpringMVC的核心配置文件中开启mvc的注解驱动,此时在HandlerAdaptor中会自动装配一个消息转换器:MappingJackson2HttpMessageConverter,可以将响应到浏览器的Java对象转换为json格式的字符串:

<!--开启MVC的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
package com.qcby.mvc.bean;

public class User {
    private Integer id;

    private String username;

    private String password;

    private Integer age;

    private String sex;

    public User(Integer id, String username, String password, Integer age, String sex) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }

    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testResponseUser}">通过@ResponseBody响应浏览器User对象</a>
</body>
</html>

将Java对象直接作为控制器方法的返回值返回,就会自动转换为json格式的字符串: 

package com.qcby.mvc.controller;

import com.qcby.mvc.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HttpController {


    @RequestMapping("/testResponseUser")
    @ResponseBody
    public User testResponseUser(){
        return new User(1001,"admin","123456",20,"男");
    }

}

 

SpringMVC处理ajax

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/axios.min.js"></script>
<body>
<h1>首页</h1>
<div id="app">
    <a @click="testAxios" th:href="@{/testAxios}">SpringMVC处理ajax</a>
</div>
<script type="text/javascript">
    new Vue({
        el:"#app",
        methods:{
            testAxios:function (event){
                axios({
                    method: "post",
                    url: event.target.href,
                    params:{
                        username:"admin",
                        password:"123456"
                    }
                }).then(function (response){
                    alert(response.data);
                });
                event.preventDefault();
            }
        }
    })
</script>
</body>
</html>
package com.qcby.mvc.controller;

import com.qcby.mvc.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HttpController {

    @RequestMapping("/testAxios")
    @ResponseBody
    public String testAxios(String username,String password){
        System.out.println(username+","+password);
        return "hello,axios";
    }
}

 

@RestController注解

@RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了@Controller注解,并且为其中的每个方法添加了@ResponseBody注解

ResponseEntity

ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文


http://www.kler.cn/a/370725.html

相关文章:

  • 【Docker】使用Dev Container进行开发
  • Android面试题
  • 【进程与线程】进程的状态
  • Jenkins与不同阶段测试的完美结合
  • AWS Lambda
  • 类模板的使用方法
  • 【30】C++子类相关
  • 大数据日志处理框架ELK方案
  • SpringBoot使用JpaRepository方法命名和@Query查询的一些复杂场景
  • js 简单模拟JSON.stringify 功能
  • 初始JavaEE篇——多线程(4):wait、notify,饿汉模式,懒汉模式,指令重排序
  • [vulnhub]Kioptrix: Level 1.2 (#3)
  • 2024年9月电子学会青少年软件编程Python等级考试(三级)真题试卷
  • 赛博威携手百度智能云,开启数字营销新未来
  • Docker Compose一键部署Spring Boot + Vue项目
  • CSS3新增长度单位
  • 在Ubuntu(Linux)系统下安装Anaconda3
  • Kubernetes固定Pod IP和Mac地址
  • 手机号二要素核验 API 对接说明
  • 【04】RabbitMQ的集群机制
  • Ajax:表单 模板引擎
  • smuge error
  • 2025秋招八股文--服务器篇
  • 我接触csdn中的c++的时间
  • 简记Vue3(二)—— computed、watch、watchEffect
  • 【蓝桥杯选拔赛真题78】python电话号码 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析