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 中的代码作为基础,并 ...
随机推荐
- jQuery之换肤与cookie插件
有时候一个网页可以有多个皮肤进行选择,也就是不同的背景,或是一整套新的css,能使整个页面变成另一种风格. 这个功能可以用jQuery来实现.外加cookie插件.有了cookie,就可以长时间的保存 ...
- kuaisupaixu
#include<stdio.h> void quiksort(int a[],int low,int high) { int i = low; int j = high; ...
- PHP 依赖注入 (转)
说这个话题之前先讲一个比较高端的思想--'依赖倒置原则' "依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高 ...
- 蓝牙的OBEX协议
1.概述 OBEX为Object Exchange,用于在蓝牙设备间传数据对象,来源于红外定义的协议,后被蓝牙采用.OBEX在蓝牙协议层中的位置如下图(在之前的OBEX版本中,OBEX是通过R ...
- 修改Wordpress目录
安装完wordpress,此时访问地址为:http://localhost/wordpress 1.不想让人知道我的文件目录,于是我把原来的wordpress改为w, 2.后台设置 3.WordPre ...
- eclipse如何修改dynamic web module version
eclipse如何修改dynamic web module version 一.修改工程属性: 右键eclipse的工程,选择属性,再选择Project Facets里面中选择Dynamic Web ...
- How to disable and clear query ranges in sysquery form
query = new query('Query name'); queryBuildDataSource = query.dataSourceTable(tableNum('table name') ...
- [LeetCode]题解(python):80-Remove Duplicates from Sorted Array II
题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Follow up for "Remov ...
- JavaScript学习之对象
JavaScript对象 一.对象简介 JavaScript 是面向对象的编程语言 (OOP).OOP 语言使我们有能力定义自己的对象和变量类型.注意:对象只是一种特殊的数据.对象拥有属性和方法. 1 ...
- 安装sqlserver2008r2 服务器配置,服务帐户配置出错,提示Sql server服务指定的凭据无效
win+X 点击运行 重置帐户密码使得sql server2008 的服务帐户名,密码与系统设置的Administrator名与密码一致,则可. 上图中有一项reporting的名字不同,该名字为自动 ...