首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Spring 4.x版本的,所以我在这里就直接将Spring升级到了4.0.8,下面我贴出我的pom文件主要的依赖:
<properties> <spring.version>4.0.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- web项目编译时需要,部署到tomcat中就不需要了 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- web项目编译时需要,部署到tomcat中就不需要了 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.3</version> </dependency> </dependencies> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories>
这里有spring,springMVC,jackson及编译时需要的servlet-api及jsp-api依赖,就没有其他多余的依赖了。因为spring是原生支持Jackson,所以我这里并没有选择使用我常用的fastjson,或者gson。当然,如果你们在自己的项目中使用的是fastjson,或gson的话,也没有关系,替换使用的话也还是很方便与简单的,这里不做详细介绍。
添加依赖后,就是配置web文件了。创建一个Restful web service与普通的springMVC项目没有什么区别,所以web配置也跟大家创建普通springMVC项目一样,简单的只需要配置一下servlet与servlet-mapping就可以了。然后就是指定springMVC配置文件,并添加到init-param或context-param中就可以了。
<context:component-scan base-package="com.yyd.spring.aop.rest.controller" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </list> </property> </bean>
@RestController public class GreetingController { private static final String template="Hello,%s"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name",defaultValue="World") String name){ return new Greeting(counter.getAndIncrement(),String.format(template, name)); } }
这里我们看到控制器上的注解由我们经常使用的@Controller变成了@RestController。@RestControllr是spring 4.x中的新注解,@RestController,表明该类的每个方法返回对象而不是视图。它实际就是@Controller和@ResponseBody混合使用的简写方法。@ResponseBody的作用是将返回的对象放入响应消息体中("其实大家都懂的,啰嗦了一句。")。
然后大家就可以启动tomcat或jetty服务器,在浏览器中输入:
http:
//localhost:8080/greeting
然后就可以看到浏览器返回的结果了,
{
"id"
:
1
,
"content"
:
"Hello, World!"
}
至此,一个简单的基于Spring 4.x的Restful web service框架就已经完成,谢谢大家,希望对大家有所帮助。
http://my.oschina.net/yuyidi/blog/352909
相关推荐
Building a RESTful Web Service with Spring 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
使用SpringBoot整合jersey 实现Restful web service.同时整合springmvc。
Building a RESTful Web Service with Spring
Starting from the basics of the philosophy behind REST, you’ll go through the steps of designing and implementing an enterprise-grade RESTful web service. Taking a practical approach, each chapter ...
构建一个简单的spring-boot的RESTful Web Service项目
Building RESTful Web Services with Spring 5(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除查看此书详细信息请在美国亚马逊官网搜索此书
RESTful Web Service 是一个使用 HTTP 和 REST 原理实现的 Web Service。通常,一个 RESTful Web Service 将定义基本资源 URI、它所支持的表示/响应 MIME,以及它所支持的操作。
利用Spring对RESTful Web Service进行简单的实现demo。
本文将介绍如何使用Spring创建Java实现的服务器端RESTful Web Services。这个例子将使用浏览器、curl和Firefox插件RESTClient作为发出请求的客户端。 本文假定您是熟悉REST基本知识的。 Spring 3的REST支持 ...
我们将学习如何使用Spring 4与REST Web Service来获取JSON响应。Spring 4 Web Service类用@RestController 进行注释, 可以取代@Controller和@ResponseBody的使用。要映射REST Web服务URL,请使用注释@...
Over 40 recipes for creating cloud-ready Java web applications with Spring MVC About This Book Configure Spring MVC to build logic-less controllers that transparently support the most advanced web ...
介绍了使用jersey集成spring与hibernate实现的restful开发
Spring Boot RESTful Web服务示例该示例项目的主要目的是演示Spring Boot的功能。 但此外,我想展示在使用Spring Boot的开发过程中可能发生的具有挑战性的问题。 第一个目标是展示如何使用嵌入式tomcat和嵌入式H2...
确保您使用的是JDK 1.8和Maven 3.x 您可以通过运行mvn clean package来构建项目并运行测试 成功构建后,可以通过以下两种方法之一运行服务: `java -jar -Dspring.profiles.active =测试目标/spring-boot-rest-...
spring-restful-service 使用 Spring Platform 1.1.0 的 RESTful Web 服务 ##特征 Spring IO 平台 1.1.0 SpringMVC 宁静的服务使用注入实现可扩展性的工厂模式异常处理日志记录使用资源集成 ##使用 Eclipse 构建...
这是一个基于 Spring MVC + Vue 的simple前后端分离web项目,电子商务,做到实现商品的展示、购买等功能。 后期会继续更新,使得项目功能更丰富。[2019/12] videos-recommender 一个简单的基于 Mahout库 的电影推荐...
gs-rest-service:构建RESTful Web服务::了解如何使用Spring创建RESTful Web服务
RESTful Web Service DemoS with Jersey,Hibernate,Mysql,Spring,JQuery,AngularJS ...跟随我创建一个完整的Web应用程序。 随便去吧! 这是有关如何逐步构建项目的文章,每个演示都是一个小项目: 1.用泽西...
Spring-Boot-Restful-Web服务