Spring Boot创建一个RESTful Web Service
来源:互联网 发布:壁纸桌面软件下载 编辑:程序博客网 时间:2024/05/31 20:52
简述
这篇文章简述了使用Spring Cloud创建一个简单的”hello world”的Restful Web Service流程。
你将构建什么
你将构建一个接收HTTP GET请求的服务: http://localhost:8080/greeting
并且在请求时返回一个JSON格式的字符串: {"id":1,"content":"Hello, World!"}
你也可以指定name
参数: http://localhost:8080/greeting?name=User
name
参数会覆盖默认的”World”值,并且体现在返回值: {"id":1,"content":"Hello, User!"}
你需要什么
1、jdk 1.8 及以上
2、Gradle 2.3+ or Maven 3.0+
3、你也可以在你的IDE中直接导入代码:
1)、Spring Tool Suite(STS)
2)、IntelliJ IDEA
Maven支持
<?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>1.5.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories></project>
构建测试工程
Create a resource representation class
package com.zooper.demo;public class Greeting { private long id; private String name; public Greeting(long id, String name) { super(); this.id = id; this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
Create a resource controller
在Spring构建RESTful web services中,Http请求被使用为一个controller.这些组件很容易被@RestController
注解识别,并且在GreetingController
中使用GET
方式请求/greeting
返
回了一个Greeting
对象:
package com.zooper.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;@RestControllerpublic 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.incrementAndGet(), String.format(template, name)); }}
Make the application executable
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@SpringBootApplication
是一个便利的注解,它添加了以下所有:
@configuration
标记的class作为一个bean资源在应用环境中定义。@EnableAutoConfiguration
告诉Spring Boot 根据classpath配置、其它beans和各种属性配置去添加beans。- 通常你会在一个Spring MVC应用中使用
@EnableWebMvc
,但是Spring Boot 在classpath中看到spring-webmvc时会自动使用该注解。 @ComponentScan
告诉Spring在当前包中查询其它的部件、配置和服务,并允许它去找到controllers.
在main()方法中调用了SpringApplication.run()方法去开始一个应用。
执行了以上操作后,你就可以访问: http://localhost:8080/greeting
或者指定name值: http://localhost:8080/greeting?name=zhangsan
返回的是一个json格式的字符串: {"id":3,"name":"Hello, zhangsan!"}
- Spring Boot创建一个RESTful Web Service
- Spring Boot 创建RESTful Web Service
- 使用spring创建一个RESTful Web Service
- spring boot demo( 获取一个RESTful web service)
- 使用spring boot快速构建一个RESTful Web Service
- Spring Boot: Building a RESTful Web Service
- Spring Boot 构建一个RESTful Web服务
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- Spring Boot 之 Consuming a RESTful Web Service
- 快速搭建RESTful Web Service(Spring Boot)
- 【Spring官方指南学习】Spring构建一个 restful web service
- Spring Boot示例 - 1. 使用Spring Boot Actuator构建RESTful web service
- 使用Spring创建满足RESTful规范的Web Service
- 构建一个基于 Spring 的 RESTful Web Service
- 快速构建一个基于Spring的RESTful Web Service
- Spring学习笔记之一:建立一个RESTful Web Service
- 搭建spring RESTful Web Service
- 使用CXF+spring+restful创建一个web的接口项目
- Ubuntu 16.04 安装 IDEA
- Mybatis中的update动态SQL语句
- 友盟iOS推送为什么有时会出现生产环境下收不到的情况?
- Spring Validator解读
- 尼姆博奕5754Life Winner Bo
- Spring Boot创建一个RESTful Web Service
- python 处理Excel 常见问题-读取Excel中时间
- 使用 python 的sklearn模块,训练模型时,出现 ValueError: min_samples_split must be at least 2 or in (0, 1], got 1 的
- Dlib格式与Opencv之间的转化
- 仿照火狐浏览器watch函数实现功能
- ES6 对象实现 Iterator 接口
- python django 重新建模
- spring-websocket 配置 websocket发送消息(群发)
- 程序员业余时间修炼指南