A Simple Resful Structured Web

1 Project creation

1.1 Single root DispatcherServlet configuration

 

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>classpath:WEB-INF/root-context.xml</param-value>  
  </context-param>
  <servlet>
       <servlet-name>mvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:WEB-INF/mvc-servlet.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>mvc</servlet-name>
       <url-pattern>/*</url-pattern>
   </servlet-mapping>
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
  
</web-app>

 

1.2 root-context configuration

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
   	<!-- Scan package does not scan controller package-->
	<context:component-scan base-package="mvc">
		<context:exclude-filter type="regex" expression="mvc.controller.*"/>   
	</context:component-scan>
	
</beans>

 

1.3 mvc-servlet configuration

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
   	<!-- Scan controller package -->
	<context:component-scan base-package="mvc.controller"/>
	
	<!-- Enable configuration that supports Restful structure, pom needs to import jackson package -->
	<mvc:annotation-driven/>
	
</beans>

 

1.4 pom configuration

 

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.8.RELEASE</version>
	</dependency>
    <dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>3.1.0</version>
	    <scope>provided</scope>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-core</artifactId>
	    <version>2.8.8</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.8.8</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-annotations</artifactId>
	    <version>2.8.8</version>
	</dependency>
  </dependencies>

 

1.5 UserController

 

 

package mvc.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import mvc.bean.User;

@RestController
@RequestMapping("user")
public class UserController
{
	private static User user = new User();
	
	@GetMapping("{id}")
	public User getUser(@PathVariable Long id)
	{
		if(user != null && id.equals(user.getId()))
		{
			return user;
		}
		
		return new User();
	}
	
	@PostMapping("add")
	public String addUser(@RequestBody User user)
	{
		this.user.setId(user.getId());
		this.user.setName(user.getName());
		return "ok";
	}
}

 

1.6 User object

 

 

package mvc.bean;

public class User
{
	private Long id;
	
	private String name;

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
	@Override
	public String toString()
	{
		return this.id + this.name;
	}
}

 

1.5 Complete project structure



 

2 Start and test the project

2.1 Startup success message

 

信息: Initializing Spring FrameworkServlet 'mvc'
June 19, 2017 7:13:49 pm org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'mvc': initialization started
June 19, 2017 7:13:49 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'mvc-servlet': startup date [Mon Jun 19 19:13:49 CST 2017]; parent: Root WebApplicationContext
June 19, 2017 7:13:49 pm org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [WEB-INF/mvc-servlet.xml]
June 19, 2017 7:13:50 pm org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
信息: Mapped "{[/user/{id}],methods=[GET]}" onto public mvc.bean.User mvc.controller.UserController.getUser(java.lang.Long)
June 19, 2017 7:13:50 pm org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
信息: Mapped "{[/user/add],methods=[POST]}" onto public java.lang.String mvc.controller.UserController.addUser(mvc.bean.User)
June 19, 2017 7:13:50 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-servlet': startup date [Mon Jun 19 19:13:49 CST 2017]; parent: Root WebApplicationContext
June 19, 2017 7:13:50 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-servlet': startup date [Mon Jun 19 19:13:49 CST 2017]; parent: Root WebApplicationContext
June 19, 2017 7:13:50 pm org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'mvc': initialization completed in 707 ms
June 19, 2017 7:13:50 pm org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
June 19, 2017 7:13:50 pm org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8032"]
June 19, 2017 7:13:50 pm org.apache.catalina.startup.Catalina start
Info: Server startup in 2239 ms
 You can see that a post and a get interface have been registered successfully.

 

2.2 Register a user


 

2.3 Get registered users

 

The data returned when the request id is not 123



 

The data returned when it is 123



 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326298130&siteId=291194637