一个简单Resful结构Web 博客分类: SpringMVC
程序员文章站
2024-02-05 16:20:34
...
1 工程创建
1.1 单根DispatcherServlet配置
<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配置
<?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"> <!-- 扫描包不扫描controller包 --> <context:component-scan base-package="mvc"> <context:exclude-filter type="regex" expression="mvc.controller.*"/> </context:component-scan> </beans>
1.3 mvc-servlet配置
<?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"> <!-- 扫描controller包 --> <context:component-scan base-package="mvc.controller"/> <!-- 启用支持Restful结构的配置,pom需要导入jackson包 --> <mvc:annotation-driven/> </beans>
1.4 pom配置
<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对象
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 项目完整结构
2 启动和测试项目
2.1 启动成功信息
信息: Initializing Spring FrameworkServlet 'mvc' 六月 19, 2017 7:13:49 下午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'mvc': initialization started 六月 19, 2017 7:13:49 下午 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 六月 19, 2017 7:13:49 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [WEB-INF/mvc-servlet.xml] 六月 19, 2017 7:13:50 下午 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) 六月 19, 2017 7:13:50 下午 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) 六月 19, 2017 7:13:50 下午 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 六月 19, 2017 7:13:50 下午 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 六月 19, 2017 7:13:50 下午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'mvc': initialization completed in 707 ms 六月 19, 2017 7:13:50 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-nio-8080"] 六月 19, 2017 7:13:50 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-nio-8032"] 六月 19, 2017 7:13:50 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 2239 ms可以看到一个post和一个get接口已经注册成功了。
2.2 注册一个用户
2.3 获取注册的用户
请求id不是123时返回的数据
是123时返回的数据
上一篇: JAVA基础 JavaC#C++C多线程