使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
2018-02-26 22:31
1466 查看
使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
这个例子创建了一个可以通过以下地址接收到HTTP GET请求的服务http://localhost:8080/checkin?name=XXX
并返回一个以下格式的JSON
{"content":"Hello XXX!"}
注解说明
@RestController:该注解由以下几个注解组成
@Retention(value=RUNTIME) @Documented @Controller @ResponseBody用于说明该类是一个接受Rest请求的控制器类
@RequestMapping(“/checkIn”)
用于说明该方法是一个用于处理资源请求的方法,处理对于HTTP请求
@RequestParam(value = “name”, defaultValue = “null”)
用于获取HTTP GET请求参数列表中name对应的值,如果为空则设置默认值为null
@SpringBootApplication
SpringBoot项目的主方法入口点,一个项目只能有一个该注解
程序代码
User.javapublic class User{ private final String content; public User(String content){ this.content = content; } public String getContent() { return content; } }
CheckInController.java
@RestController public class CheckInController { private static final String template = "Hello,%s!"; @RequestMapping("/checkIn") public User checkIn(@RequestParam(value = "name", defaultValue = "null") String name){ return new User(String.format(template, name)); } }
CheckinApplication.java
@SpringBootApplication public class CheckinApplication { public static void main(String[] args) { SpringApplication.run(CheckinApplication.class, args); } }
运行结果
相关文章推荐
- 使用Gradle创建一个最简单的Spring Boot项目
- IDEA-------(二)快速创建一个简单的Spring boot项目
- 使用IDEA创建一个Spring Boot项目
- 使用spring创建一个RESTful Web Service
- 创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)
- 使用spring boot快速构建一个RESTful Web Service
- SpringBoot入门学习一(如何使用IDEA创建一个SpringBoot应用)
- 基于maven使用IDEA创建springboot多模块项目
- SpringCloud微服务系列(2): 创建一个基于Springboot的RESTFul服务
- 使用 Intellij idea Springboot maven 的一个小小的问题
- 使用Spring来创建一个简单的工作流引擎
- 快速构建一个基于Spring的RESTful Web Service
- SpringBoot IntelliJ创建简单的Restful接口
- 使用Spring来创建一个简单的工作流引擎
- 使用Spring来创建一个简单的工作流引擎
- Spring Boot 创建RESTful Web Service
- 使用Spring Boot创建一个应用
- 构建一个基于 Spring 的 RESTful Web Service