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

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

STS安装参考

目标

在浏览器中输入url:

http://localhost:8080/greeting

访问后得到结果:

{"id":1,"content":"Hello, World!"}

可以在url中带上参数:

http://localhost:8080/greeting?name=User

带上参数后的结果:

{"id":1,"content":"Hello, User!"}

开始

新建项目,通过菜单“File->New->Spring Starter Project” 新建。

在“New Spring Starter Project”对话框里自定义打上项目名,Atifact,Group,Package后,点Next。

在“New Spring Starter Project Dependencies”中,选择Spring Boot Version,把Web组件勾上,表示要构建支持RESTful的服务。Web组件中包含的内容可以在提示框中看到,可以支持RESTful和Spring MVC。

点Finish完成向导,等待STS构建完成,可以看右下角的构建进度。

待构建完成后,在STS左侧的"Package Explorer"中就能看到整个项目的结构了。

建完项目后, 首先创建一个该服务响应的json数据对应的对象,右击包“com.example.demo”,新建一个Class,取名Greeting,然后点Finish 。

Greeting.java的代码为:

package com.example.demo;

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 this.id;
    }

    public String getContent() {
        return this.content;
    }
}

按照最终访问url响应的结果,写上对应的字段已经他们的getter。

接着为了完成响应,创建对应的Controller,创建一个名为GreetingController的类,方法同上,点Finish。

GreetingController.java的代码为:

package com.example.demo;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;    

@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", required=false, defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

在Class定义上面,我们直接使用了@RestController注释,直接表示这是一个提供RESTful服务的Controller,这样在所有与url关联的方法上就不需要指定@ResponseBody来明确响应的数据类型,直接就会响应json数据。

在greeting方法的上面使用@RequestMapping将访问的url和处理方法进行关联,默认情况下支持GET,PUT,POST,DELETE所有的HTTP Method,如果要指定GET,可以写成@RequestMapping(method=GET)。

在greeting方法的参数中,将方法的参数和url中的参数进行了绑定,可以通过required指明参数是否必须,如果指明了true,那么要根据情况把defaultValue指定默认值,否则会报异常。

最后,以Spring Boot App方式运行。

运行后,在浏览器里访问url就能看到结果了。

url

http://localhost:8080/greeting?name=bobeut

结果:

{"id":1,"content":"Hello, bobeut!"}

End

时间: 2024-08-04 03:05:34

用Spring Tools Suite(STS)开始一个RESTful Web Service的相关文章

3.消费一个RESTful Web Service

这节课我们根据官网教程学习如何去消费(调用)一个 RESTful Web Service . 原文链接 https://spring.io/guides/gs/consuming-rest/ 本指南将引导您完成创建使用RESTful Web服务的应用程序的过程. 我们将会创建什么? 您将构建一个使用Spring RestTemplate的应用程序来检索http://gturnquist-quoters.cfapps.io/api/random中的随机Spring Boot引用. 你需要准备什么?

用Spring构建一个RESTful Web Service

本教程将手把手教你用Spring构建一个"hello world" RESTful Web Service. 你将构建什么 你将构建一个Web Service,用于接收HTTP GET请求,请求地址: http://localhost:8080/greeting 请求响应返回一段JSON格式的问候语: {"id":1,"content":"Hello, World!"} 你可以在请求中添加一个可选参数:name,定制问候语:

在GlassFish应用服务器上创建并运行你的第一个Restful Web Service【翻译】

前言 本人一直开发Android应用,目前Android就业形势恶劣,甚至会一路下滑,因此决定学习服务器开发.采用的语言是java,IDE是Intellij,在下载Intellij的同时看到官网很多优秀的guide文章,于是按照guide成功完成了一个RESTful的demo.官方文档非常简洁,给我带来了很大的帮助,于是翻译之,希望对其他不愿意看原文的人有所帮助.由于水平有限,读者发现错误请指正,谢谢. 原文地址: https://www.jetbrains.com/help/idea/2016

Spring Tools Suite (STS) 简介

首先,sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用. 官方页面.下载地址(3.8.1 win x64). 其次,没什么好介绍的,用一下就明白了. 创建Spring应用的过程同 https://start.spring.io/ 中所示--实际上访问的接口一致. 下载之后,可以看到一个sts-bundle,里面有三个文件夹,一个法律信息,一个tc Server,一个sts. 注意,文件夹名称越短越好,否则会出错. 另外,note.txt是我自己

Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http://localhost:9000/rs/roomservice/room/001/ 为001号房间的信息, http://localhost:9000/rs/roomservice/room/001/person 为在001号房间主的人的列表 在Eclipse中新建一个Java Project (可

Spring 4.x实现Restful web service

首先我们还是跟之前一样,创建一个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> &

【转】Spring 4.x实现Restful web service

http://my.oschina.net/yuyidi/blog/352909 首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Spring 4.x版本的,所以我在这里就直接将Spring升级到了4.0.8,下面我贴出我的pom文件主要的依赖: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3

JAX-RS 方式的 RESTful Web Service 开发

JAX-RS 方式的 RESTful Web Service 开发 ——基于 CXF+Spring 的实现 Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 HTTP 协议规范的 RESTful 风格. SOAP 方式的 web service 已经很成熟了,应用也很广,已经成为 Web Service 的工业标准.不过 RESTful Web Service 现在势头越来越猛,特别是其灵活性以及与 Ajax 的完美结合,让人爱不释手,很有必要了解一下.

RESTful Web Service实战 小结1

1 REST的基本实现形式HTTP+URI+XML,但不是唯一形式.XML后来被Json格式替代.REST是一中架构风格(Representational State Transfer,表述性状态转移),而不是具体的实现形式. 2 RESTful Web Service是一种遵守REST风格的WEB服务.是一种ROA(Resource-Oriented Architecture,面向资源的架构)应用. 主要特点是方法信息存在于HTTP的方法中(比如GET/PUT),作用域存在于URI中.作用域信