Chapter 7: SpringMVC's HttpMessageConverter

1. Introduction to HttpMessageConverter

①HttpMessageConverter, a message information converter, converts a request message into a java object, or converts a java object into a response message

②HttpMessageConverter provides two annotations and two types

@RequestBody,@ResponseBody,RequestEntity,ResponseEntity

2. @RequestBody POST request

@RequestBody can get the request body, you need to set a formal parameter in the controller method, use @RequestBody to identify, the request body of the current request will assign a value to the formal parameter identified by the current annotation

①index.html

 ②IndexController

 Output request body: requestBody: username=666&password=666

3.@RequestEntity

RequestEntity encapsulates a type of request message. It is necessary to set this type of parameter in the controller method parameter. The request message of the current request will be assigned to this parameter. The request header information can be obtained through getHeaders(), and the request header information can be obtained through getBody () Get request body information.

①index.html

 ②IndexController

@RequestMapping("/testRequestEntity")
public String testRequestBody(RequestEntity<String> requestEntity){
   System.out.println("requestHeader:"+requestEntity.getHeaders());
   System.out.println("requestBody:"+requestEntity.getBody());
   return "success";
}

request header

[host:"localhost:8080",connection:"keep-alive",cache-control:"max-age=0",sec-ch-ua:"" Not A;Brand";v="99",…]

request body

username=1123&password=666

4. Through HttpServletResponse

@RequestMapping("/testResponse")
public void testRes(HttpServletResponse response) throws IOException {
   response.getWriter().print("hahahaha");
}

turn out

 

5.@ResponseBody

@ResponseBody is used to identify a controller method, and the return value of the method can be directly responded to the browser as the response body of the response message. When @ResponseBody is annotated, return "success"; instead of executing the view name to load the html page, the current response body is "success" and the content is loaded on the page.

@RequestMapping("/testResponseBody")
@ResponseBody
public String testResBoy(){
   return "success内容";
}

the effect is 

 6. SpringMVC processes json--java objects into json format strings

①Create the javaBean object of User.class under the bean package

 ② Write method @ResponseBody annotation identification in the controller

@RequestMapping("/testResponseUser")
@ResponseBody
public User testResUser(){
   return new User(101,"daming","666",12,"男");
}

③The test will report error 500

 ④ Convert to json object

Import jackson dependencies in pom.xml

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

Enable annotation driver in springMVC.xml

 <!--开启mvc注解驱动-->
<mvc:annotation-driven />

test

 

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/131067948
Recommended