Review the traditional annotation SpringMVC architecture
1. The pom
<! -- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2. RELEASE</version>
</dependency>
<! -- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2. RELEASE</version>
</dependency>
<! -- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Copy the code
2. The new package in the Java directory and AbstractAnnotationConfigDispatcherServletInitializer implementation class
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcInit extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protectedClass<? >[] getRootConfigClasses() {return null;
}
@Override
protectedClass<? >[] getServletConfigClasses() {return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"}; }}Copy the code
Explain why use implementation AbstractAnnotationConfigDispatcherServletInitializer to initialize the DispatcherServlet:
In the Servlet3.0 environment, the container will be in the classpath lookup javax.mail. Servlet. ServletContainerInitializer interface classes, if found, you can use it to configure the servlet container. Spring provides the implementation of this interface, called SpringServletContainerInitializer, this class, in turn, will find implementation WebApplicationInitializer classes and the configuration tasks to them. Spring3.2 based implementation, introduces a convenient WebApplicationInitializer called AbstractAnnotationConfigDispatcherServletInitializer, When our class extends the AbstractAnnotationConfigDispatcherServletInitializer and deploy it to Servlet3.0 container, the container automatically discover it, and use it to configure the Servlet context.
3. Create RootConfig
import org.springframework.context.annotation.Configuration;
@Configuration
public class RootConfig {}Copy the code
@Override
protectedClass<? >[] getRootConfigClasses() {return new Class[] {RootConfig.class};
}
Copy the code
4. Create the WebConfig configuration
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc // Start springMVC component driver < MVC :annotation-driven>
@ComponentScan("com.demo")
public class WebConfig implements WebMvcConfigurer {}Copy the code
@Override
protectedClass<? >[] getServletConfigClasses() {return new Class[] {WebConfig.class};
}
Copy the code
5. The controller
@RequestMapping("/hello")
@RestController
public class HelloController {
@GetMapping
public String hello(a) {
return "hello"; }}Copy the code
7. Test
8. Return objects
public class User {
private int id;
private String name;
public User(int id, String name) {
super(a);this.id = id;
this.name = name;
}
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}Copy the code
@RequestMapping("/hello")
@RestController
public class HelloController {
@GetMapping
public String hello(a) {
return "hello";
}
@GetMapping("/user")
public User getUser(a) {
return new User(30."luopeng"); }}Copy the code
9. The test
The solution was
<! -- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
Copy the code