第一个 Spring Boot 程序 : 构建 RESTful 服务
来源:互联网 发布:u center软件 编辑:程序博客网 时间:2024/04/29 02:34
把我们基础的项目来升级一点点,还是照着文档来 https://spring.io/guides/gs/rest-service/
web.xml 没有变化
项目分成了几个包
实体类 Greeting
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
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;import com.lzf.bean.Greeting;@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));}}
main 函数
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);}}
运行
http://localhost:8080/greeting ----> {"id":1,"content":"Hello, World !"}
http://localhost:8080/greeting?name=user ---> {"id":2,"content":"Hello, user !"}
确实方便,没写一个xml
0 0
- 第一个 Spring Boot 程序 : 构建 RESTful 服务
- 使用Spring构建第一个RESTful服务
- spring boot 用maven搭建第一个RESTful Web 服务
- Spring Boot 构建一个RESTful Web服务
- 第一个 Spring Boot 程序
- 第一个Spring Boot程序
- 第一个Spring-boot程序
- 构建第一个spring boot应用程序
- spring boot 第一篇 构建第一个spring boot工程
- 第一个 Spring Boot 程序 : 文件上传
- 第一章 第一个spring boot程序
- 用Marven构建第一个Spring Boot应用
- Spring Boot使用Swagger2构建RESTful文档
- Spring Boot构建RESTful API与单元测试
- Spring Boot构建RESTful API与单元测试
- 三、Spring Boot构建RESTful API
- Spring Boot 构建 Restful API 和测试
- Spring Boot构建RESTful API与单元测试
- VMvare 无法进入BIOS,设置启动项的解决方法
- 获取请求ip与端口号
- KL散度
- 数据结构基础(一)
- 关于int除以int容易碰到的小问题
- 第一个 Spring Boot 程序 : 构建 RESTful 服务
- 转 侵立删
- Linux下简单的socket通信
- 【python】文件内容拆分
- 微信web开发者工具,登陆二维码页面排版乱了,扫码也登陆不上
- 鱼眼校正之类2
- MySQL不重复插入,记录不存在则插入,存在则更新
- [源码学习] -- yii2源码学习笔记(一) -- 目录结构了解
- CryptoJS加密库使用及bug解决