一、构建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的更多相关文章

  1. Building a RESTful Web Service(转)

    Building a RESTful Web Service This guide walks you through the process of creating a "hello wo ...

  2. 【转】Building a RESTful Web Service

    目标 构建一个service,接收如下HTTP GET请求: [plain] view plain copy   http://localhost:8080/greeting 并返回如下JSON格式的 ...

  3. Getting Started · Building a RESTful Web Service

    Getting Started · Building a RESTful Web Service undefined

  4. Spring起步(一)Building a RESTful Web Service

    http://spring.io/guides/gs/rest-service/ 先放链接. 这个很小很小的一个功课,我却遇到了各种各样的奇葩错误,折腾了两天才弄好. 想要开始的话,需要一些准备工具 ...

  5. Building a RESTful Web Service

    Reference: https://spring.io/guides/gs/rest-service/ 参照上述链接进行操作,使用gradle build. 因为total new to this. ...

  6. 用Spring Tools Suite(STS)开始一个RESTful Web Service

    spring.io官方提供的例子Building a RESTful Web Service提供了用Maven.Gradle.STS构建一个RESTFul Web Service,实际上采用STS构建 ...

  7. Spring Boot发布和调用RESTful web service

    Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...

  8. 构建一个基于 Spring 的 RESTful Web Service

    本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...

  9. Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

    准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...

随机推荐

  1. php中json_decode()和json_encode()的使用方法

    php中json_decode()和json_encode()的使用方法 作者: 字体:[增加 减小] 类型:转载   json_decode对JSON格式的字符串进行编码而json_encode对变 ...

  2. Unity3d中C#使用指针(Unsafe)的办法(转)

    近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...

  3. svn local obstruction, incoming add upon merge

    http://little418.com/2009/05/svn-local-obstruction-incoming-add-upon-merge.html If you've found this ...

  4. 泌尿系统 Excretory system

    https://zh.wikipedia.org/wiki/泌尿系统 泌尿系統,有時也歸類於排泄系統(Excretory system)的一部分,負責尿液的產生.運送.儲存與排泄.人類的泌尿系統包括左 ...

  5. 特征向量-Eigenvalues_and_eigenvectors#Graphs

    https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A               {\displaystyle A} ...

  6. maven 添加本地jar

    方式一 Xml代码 <dependency> <groupId>org.apache</groupId> <artifactId>test</ar ...

  7. [转载]推荐不伤眼睛的文字背景色 VS背景色

    天天使用电脑要主要保护眼睛.下面介绍下不伤眼睛的文字背景色 苹果绿 RGB 204,255,204 #CCFFCC 杏仁黄 rgb 250 249 222 #FAF9DE 青草绿 rgb 227 23 ...

  8. PIVOT&UNPIVOT

    如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...

  9. php——n维数组的遍历——递归

    <?php /**** ****/function digui($arr){    foreach($arr as $key => $value)    {        if(is_ar ...

  10. 设计模式:享元模式(Flyweight)

    定   义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...