SpringMVC response processing

1. Traditional synchronous business data response

Spring responds data to the client, which is mainly divided into two parts:
⚫ Traditional synchronization method: Prepare the model data and display it after jumping to the execution page. This method is used less and less. For historical reasons, some old projects still In use;
⚫ Asynchronous method of front-end and back-end separation: the front-end uses Ajax technology + Restful style to interact with the server mainly in Json format, which is almost always the way at present.

In the data response of traditional synchronization business, SpringMVC involves the following four forms:
⚫ Request resource forwarding;
⚫ Request resource redirection;
⚫ Respond to model data;
⚫ Directly write back data to the client;

1. Request resource forwarding

insert image description here

2. Request resource redirection

insert image description here

3. Response model data

The essence of response model data is also forwarding, and model data can be prepared when forwarding

@GetMapping("/forward5")
public ModelAndView forward5(ModelAndView modelAndView){
    
    
	// 准备JavaBean模型数据
	User user = new User();
	user.setUsername("haohao");
	// 设置模型
	modelAndView.addObject("user",user);
	// 设置视图
	modelAndView.setViewName("/index.jsp");
	return modelAndView;
}

4. Write back data directly

The string returned to the client directly through the return value of the method, but the default method return value of SpringMVC is a view, which can be displayed through the @ResponseBody annotation to inform the return value here not to be processed by the view, but to be processed in the form of the response body

@GetMapping("/response2")
@ResponseBody
	public String response2() throws IOException {
    
    
	return "Hello haohao!";
}

2. Separation of front-end and back-end asynchronous business data response

In fact, the write-back data here is the same as the grammatical method of writing back data to the client above, except for the following differences: ⚫ Write-back
data in synchronous mode is to respond the data to the browser for page display, and return data in asynchronous mode Write data is generally written back to the Ajax engine, that is, whoever accesses the server, the server will respond to the data to whom⚫
The data written back in synchronous mode is generally some strings without a specific format, while the data written back in asynchronous mode mostly Json format string

To write back ordinary data, use the @ResponseBody annotation method to return the string directly, which is not explained here;

Write back the string in Json format, that is, directly concatenate the string in Json format or use a tool to convert the JavaBean into a string in Json format and write it back

@GetMapping("/response3")
@ResponseBody
public String response3(HttpServletResponse response) {
    
    
	return "{\"username\":\"haohao\",\"age\":18}";
}
@GetMapping("/response4")
@ResponseBody
public String response4() throws JsonProcessingException {
    
    
	// 创建JavaBean
	User user = new User();
	user.setUsername("haohao");
	user.setAge(18);
	// 使用Jackson转换成json格式的字符串
	String json = new ObjectMapper().writeValueAsString(user);
	return json;
}

When explaining that SringMVC receives request data, the Json format string submitted by the client is also manually converted into JavaBean using Jackson

When we use @RequestBody, we directly use JavaBean to receive the data in Json format. The principle is that the bottom layer of SpringMVC helps us do the conversion. Here @ResponseBody can also automatically convert JavaBean into Json format string for us to respond

@GetMapping("/response5")
@ResponseBody
public User response5() throws JsonProcessingException {
    
    
	// 创建JavaBean
	User user = new User();
	user.setUsername("haohao");
	user.setAge(18);
	// 直接返回User对象
	return user;
}

The @ResponseBody annotation is optimized. When developing the front-end and back-end separation, each method of the Controller directly writes back the data, so each method must write @ResponseBody, you can write @ResponseBody to the Controller, then the Controller All methods in have the function of returning response body data

@Controller
@ResponseBody
public class UserController{
    
    

	@GetMapping("/response7")
	public ResultInfo response7() {
    
    
		// 省略其他代码
		return info;
	}
	
	@GetMapping("/response5")
	public User response5() throws JsonProcessingException {
    
    
		//省略其他代码
		return user;
	}
	
	// ... 省略其他方法 ...
}

For further optimization, you can use @RestController to replace @Controller and @ResponseBody, the functions of these two annotations inside @RestController

@RestController
public class UserController{
    
    

	@GetMapping("/response7")
	public ResultInfo response7() {
    
    
		// 省略其他代码
		return info;
	}
	
	@GetMapping("/response5")
	public User response5() throws JsonProcessingException {
    
    
		//省略其他代码
		return user;
	}
	
	// ... 省略其他方法 ...
}

Guess you like

Origin blog.csdn.net/qq_36602071/article/details/129826497