Building a RESTful Web Service Using Spring Boot In Eclipse
一、构建restful web service
创建Maven的java web工程,maven的pom文件加入依赖包
创建包hello
Greeting.java
package hello; public class Greeting { private final long id;
private final String content; public Greeting(long id, String content) {
this.id = id;
this.content = content;
} public long getId() {
return id;
}
public String getContent() {
return content;
}
}
创建Controller
package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class GreetingController { private static final String TEMPLATE = "Hello, %s!";
private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting")
public @ResponseBody Greeting greeting(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(TEMPLATE, name));
}
}
说明:
@RequestMapping默认接受GET、POST、PUT等所有方法
@RequestBody 表示直接以HTTP Response Body返回,而不是转到View试图层渲染后再返回,这也是Spring MVC 和 Restful web service的最大区别
创建执行器
package hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan; @ComponentScan
@EnableAutoConfiguration
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
以Spring boot app方式执行,这样就不需要部署到服务器里面了,加快开发效率
访问地址:
http://localhost:8080/greeting?name=loull
页面显示:
{ "id": 4,
"content": "Hello, loull!" }
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M6</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</project>
二、spring boot介绍
Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务。你可通过它来选择不同的 Spring 平台。可创建独立的 Java 应用和 Web 应用,同时提供了命令行工具来允许 'spring scripts'.
下图显示 Spring Boot 在 Spring 生态中的位置:
Spring Boot是Spring社区较新的一个项目。该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring 进行入门体验,让Java开发也能够实现Ruby on Rails那样的生产效率。为Spring生态系统提供了一种固定的、约定优于配置风格的框架。
Spring Boot具有如下特性:
- 为基于Spring的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
- 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
- Spring Boot并不是不对Spring功能上的增强,而是提供了一种快速使用Spring的方式。
Spring Boot CLI是一个命令行工具。使用这个命令行工具可以运行Spring脚本。那么什么是Spring脚本呢?Spring脚本使用Groovy编写的脚本代 码。编写好Spring脚本之后,使用Spring Boot CLI就可以在命令行下运行。是不是很酷?
参考:
http://spring.io/guides/gs/rest-service/
Spring Boot
http://rockingware.com/2013/12/touch-spring-boot.html
Building a RESTful Web Service Using Spring Boot In Eclipse的更多相关文章
- Building a RESTful Web Service(转)
Building a RESTful Web Service This guide walks you through the process of creating a "hello wo ...
- 【转】Building a RESTful Web Service
目标 构建一个service,接收如下HTTP GET请求: [plain] view plain copy http://localhost:8080/greeting 并返回如下JSON格式的 ...
- Getting Started · Building a RESTful Web Service
Getting Started · Building a RESTful Web Service undefined
- Spring起步(一)Building a RESTful Web Service
http://spring.io/guides/gs/rest-service/ 先放链接. 这个很小很小的一个功课,我却遇到了各种各样的奇葩错误,折腾了两天才弄好. 想要开始的话,需要一些准备工具 ...
- Building a RESTful Web Service
Reference: https://spring.io/guides/gs/rest-service/ 参照上述链接进行操作,使用gradle build. 因为total new to this. ...
- 用Spring Tools Suite(STS)开始一个RESTful Web Service
spring.io官方提供的例子Building a RESTful Web Service提供了用Maven.Gradle.STS构建一个RESTFul Web Service,实际上采用STS构建 ...
- Spring Boot发布和调用RESTful web service
Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service
准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...
随机推荐
- 【IOS笔记】View Controller Basics
View Controller Basics 视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...
- regardless of how many processors are devoted to a parallelized execution of this program
https://en.wikipedia.org/wiki/Amdah's_law Amdahl's law is often used in parallel computing to predic ...
- JQuery..bind命名空间
先看手册,由于bind方法有三个参数(type,[data],fn),所以手册上这么介绍: .bind() 方法是用于往文档上附加行为的主要方式.所有JavaScript事件对象, 比如focus, ...
- 地图API使用文档-以腾讯地图为例
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- protobuf序列化、反序列化
引用dllprotobuf-net.rar /// <summary> /// buf序列化 /// </summary> public static String Seria ...
- DependencyProperty深入浅出
写这篇心得之前,看到博友一句话:需求是推动发展的原动力. 说得好,说的棒,说到了点子上,说到了心里去: 好我们开始 最初的世界是简单的,甚至比单细胞动物还简单: 普通属性定义 public class ...
- WordPress显示备案号
备案时,需要显示备案号,而wordpress默认模板本身不带这个信息,为了更快速应付备案,解决方案如下: 根据wp-config.php的提示 .......... /** * zh_CN本地化设置: ...
- Nodejs电影建站开发实例(上)
网站环境:使用express框架.bootstrap样式.jade模板.mongoose数据库 npm insatll express -g npm insatll jada -g npm insat ...
- Android笔记:string-array数据
把相应的数据放到values文件夹的arrays.xml文件里 String数组 <?xml version="1.0" encoding="utf-8" ...
- LoadingView 自定义加载图片
#import <UIKit/UIKit.h> @interface LoadingView : UIView @property (nonatomic,strong) NSMutable ...