Spring MVC Tutorial 5 [Restful and Interceptors]

Keywords: html5 JSP Mobile JSON

Restful Style

Restful is a software design specification and a specification for data interaction between client and server.In the early development of web pages using jsp pages, data interaction was basically through form submission, then transmission through built-in objects. When HTML5 was rising, mobile Internet was rising, and web site back-end services, not only PC-side web pages, but also mobile-side data display, applets, HTML5 pages and so on.If multiple terminals (Android, iOS, applets, Pad, HTML5 pages) are required to share a single backend, JSON is generally the mainstream solution.RESTful, on the other hand, standardizes the requested URL, noting that RESTful is a specification, not a technology.

In Restful:

1. A URL operates on a resource
2. The requested URL must not have verbs
3. Describe request behavior using HTTP
In the restful interface, all methods return json data without returning a page (modelandview), so the @restponsebody annotation is added to all methods.A simple alternative is to use @RestController instead of @Controller, which is a combination of @Controller and @ResponseBody:

case

package com.sxt;


import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.bean.Book;

@RestController
public class Conreoller {
	@PostMapping("/book")
	@ResponseBody
	public void getbook(@RequestBody Book book){
		System.out.println(book);
	}
	@GetMapping("/book/{id}")
	@ResponseBody
	public void getbookid(@PathVariable Integer id){
		System.out.println(id);
	}
	@DeleteMapping("/book/{id}")
	@ResponseBody
	public void getbookid1(@PathVariable Integer id){
		System.out.println(id);
	}
	
	@PutMapping("/book")
	@ResponseBody
	public void getbookid1(@RequestBody Book book){
		System.out.println(book);
	}
	
}

2. Interceptors

1. Introduction

Interceptors in SpringMVC correspond to filters, filters, and interceptors in the Web Foundation:

Sequence Number Difference
1 In general, if you use the SpringMVC framework and then have interception requirements, it is recommended that interceptors be used instead of filters
2 Filters depend on Servlet containers, while interceptors come with SpringMVC and do not depend on containers
3 Interceptors are more powerful because they are an AOP-style filter

Use (Define Interceptors)

package com.sxt.fifle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * custom interceptor
 * @author Administrator
 *
 */
public class MyFifle implements HandlerInterceptor{
	/**
	 * Method invoked before entering Handler
	 * Handle:
	 *    Used for authentication and authorization
	 *    For example, confirm whether the current request is logged on, and if it is logged on, use the method; otherwise, intercept the jump back to the logon interface
	 * @return
	 *    true Let go
	 *    false intercept
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("Executed");
		return true;
	}
	/**
	 * After entering Handler, execute before returning ModelAndView object
	 * Adjustable views can be modified
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("Executed in the middle");
	modelAndView.setViewName("/user.jsp");
		
	}
	/**
	 * Execute this method after executing the Handler.
	 * Scenarios:
	 *    Unified exception handling, Unified log processing, Resource release
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("Executed at the end");
		
	}

}

The preHandle method of the first interceptor returns false (none of the following methods have been executed):
preHandle
The preHandle method of the first interceptor returns true,
The preHandle method of the second interceptor returns false, at which point the afterCompletion method of the first interceptor executes and the preHandle method of the second interceptor executes.
Rule: 1. The postHandle method will not execute until all interceptor preHandle methods return true.
2. When the interceptor's own preHandle method returns true, the subsequent interceptor will execute, and its own afterCompletion will execute.
3. The order in which interceptors are executed is related to the order in which they are defined in the xml.(preHandle executes in the order defined, postHandle and afterCompletion execute in the reverse order defined)

Configure Interception Conditions

	<!--How to turn on SpringMVC comments-->
	<mvc:annotation-driven >
	</mvc:annotation-driven>
		<!--Turn on scan-->
	<context:component-scan base-package="com.sxt"/>
	<!--Interceptor Configuration-->
	<mvc:interceptors>
		<mvc:interceptor>
		<!-- ** indicates the current directory and its subdirectory path-->
				<mvc:mapping path="/**"/>
				<!--Full path of custom interceptor-->
				<bean class="com.sxt.fifle.MyFifle"/>
		</mvc:interceptor>
	</mvc:interceptors>

test


But the page was modified in the interceptor


How Interceptor works

Posted by omarh2005 on Sun, 19 May 2019 06:03:37 -0700