当然写本文的目的不是为了速度,只是表明现在构建一个Spring web mvc Rest风格的HelloWorld应用会很简单。不过如果看过Spring Boot这个项目,可能只需要最多3分钟就能构建一个简单的Rest风格应用。回头研究下,然后分享下。
我的构建环境
JDK 7
Maven 3
Servlet3容器
创建项目
首先使用Maven创建一个普通Maven应用即可,不必是web的。
添加依赖
- <!-- servlet 3 -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.0.1</version>
- </dependency>
- <!--spring context -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.0.0.RELEASE</version>
- </dependency>
- <!--spring webmvc -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>4.0.0.RELEASE</version>
- </dependency>
- <!--jackson -->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.2.3</version>
- </dependency>
servlet3依赖scope是provided表示环境提供,然后添加spring-context-support和spring-webmvc依赖,最后用于json的jackson依赖。非常简单明了。
添加maven插件
为了方便测试,添加jetty的maven插件,这样直接使用mvn jetty:run即可运行。
- <build>
- <finalName>springmvc</finalName>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>8.1.8.v20121106</version>
- <configuration>
- <reload>manual</reload>
- <webAppConfig>
- <contextPath>/${project.build.finalName}</contextPath>
- </webAppConfig>
- <connectors>
- <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
- <port>9080</port>
- <!--<maxIdleTime>60000</maxIdleTime>-->
- </connector>
- </connectors>
- </configuration>
- </plugin>
- </plugins>
- </build>
实体
- package com.sishuok.entity;
- import java.io.Serializable;
- /**
- * <p>User: Zhang Kaitao
- * <p>Date: 13-12-19
- * <p>Version: 1.0
- */
- public class User implements Serializable {
- 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 boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- User user = (User) o;
- if (id != null ? !id.equals(user.id) : user.id != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- return id != null ? id.hashCode() : 0;
- }
- }
控制器
- package com.sishuok.controller;
- import com.sishuok.entity.User;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * <p>User: Zhang Kaitao
- * <p>Date: 13-12-19
- * <p>Version: 1.0
- */
- @RestController
- @RequestMapping("/user")
- public class UserController {
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- public User view(@PathVariable("id") Long id) {
- User user = new User();
- user.setId(id);
- user.setName("zhang");
- return user;
- }
- }
pringMVC注解风格配置
- @Configuration
- @EnableWebMvc
- @ComponentScan
- public class AppConfig {
- }
具体含义请参考《Spring4新特性——Groovy Bean定义DSL》部分。
Servlet3容器启动初始化器
在Servlet容器启动时编程式注册Servlet
- package com.sishuok;
- import org.springframework.web.WebApplicationInitializer;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
- import org.springframework.web.servlet.DispatcherServlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRegistration;
- /**
- * <p>User: Zhang Kaitao
- * <p>Date: 13-12-19
- * <p>Version: 1.0
- */
- public class AppInitializer implements WebApplicationInitializer {
- @Override
- public void onStartup(ServletContext servletContext) throws ServletException {
- AnnotationConfigWebApplicationContext webApplicationContext =
- new AnnotationConfigWebApplicationContext();
- webApplicationContext.register(AppConfig.class);
- DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
- ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
- dynamic.addMapping("/");
- }
- }
然后运行 mvn jetty:run运行即可,浏览器输入如下地址即可得到我们的json数据。
http://localhost:9080/springmvc/user/1
参考示例的github地址:springmvc-rest-helloworld
非常简单的一个Rest风格的web应用就搭建完了,接下来再完善即可。
下一篇将介绍使用Spring Boot 2分钟构建spring mvc REST风格HelloWorld。
相关推荐
NULL 博文链接:https://jinnianshilongnian.iteye.com/blog/1997192
Spring3MVC-REST-HelloWorld 实例简单代码
包括:注解式开发HelloWorld、处理器定义、请求映射、REST风格支持、请求数据映射、数据绑定等诸多内容 第四章:数据类型转换 包括:简介、内建类型转换器、自定义类型转换器等 第五章:数据格式化 包括:简介、...
spring4-mvc-docker-hello-world Spring Mvc 4 Docker Hello世界示例,带有jsp视图和rest控制器以及自动装配服务。 乌尔 运行IT测试 mvn clean verify 作者 Juan EnriqueAlcázarSolomando-初期工作 执照 此项目...
本节中的例子将演示Spring 3环境的创建过程,并创建一个可以部署到Tomcat中的“Hello World”应用。然后我们再完成一个更复杂的应用来了解Spring 3 REST支持的重要概念,如多种MIME类型表示支持和JAXB支持。另外,...
你好休息使用 Spring 4 和 Gradle 的 Hello World 应用程序。 使用./gradlew eclipse项目加载到./gradlew eclipse 使用./gradlew idea项目加载到 Intellij 使用./gradlew tomcatRun在 tomcat 上运行
本指南将引导您完成使用Spring创建“ Hello,World”超媒体驱动的REST Web服务的过程。 是REST的重要方面。 它使您可以构建使客户端和服务器在很大程度上分离并使其独立发展的服务。 为REST资源返回的表示形式不仅...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 示例:RequiredAnnotationBeanPostProcessor 81 4.8.2 使用BeanFactoryPostProcessor 自定义配置元数据. 81 4.8.2.1 示例:...
路线图: 1 Hello world(简单的JSP) 2 Hello world(简单servlet) 3 Hello world(Spring MVC) 4 Rest Hello World(Spring MVC) 5 WebSocket服务6 Angular(或其他框架)前端Web应用程序。 7 WebApp的“ hello...
3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................
Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...
Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...