博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring官方指南学习】Spring构建一个 restful web service
阅读量:4070 次
发布时间:2019-05-25

本文共 3632 字,大约阅读时间需要 12 分钟。

【Spring学习笔记,稍作记录,主要参考Spring官网 】

构建一个restful web service

理解如何用spring去构建一个简单的 “hello world” Restful web service.


【即将创建】

  • 创建HTTP GET的request请求: 

    返回一个JSON格式的响应: 
    {“id”: 1, “content”: “Hello, World!”}

  • 定制响应的内容,在请求地址里面加入一个可选的name参数: 

    在返回的响应内容中,name参数将会替代掉默认的”World”: 
    {“id”:1, “content”: “Hello, User!”}


【环境需要】

  • 大约15min
  • JDK1.8+
  • Maven 3.0+
  • IDE:eclipse或者idea, 此处推荐STS(Spring Tool Suite)

【实现步骤】

  1. 构建工程

      2. Maven, pom文件
4.0.0
org.springframework
gs-rest-service
0.1.0
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.jayway.jsonpath
json-path
test
1.8
org.springframework.boot
spring-boot-maven-plugin
spring-releases
https://repo.spring.io/libs-release
spring-releases
https://repo.spring.io/libs-release
      3. 创建model、controller类
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;    }}
package hello;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));    }}

在完成这一步之后,其实这个restful service就可以运行了,按照老的方式就是打成war 包,发布 到服务器上就可以了。

但是对月开发来说,发布步骤太繁琐了,下面Spring boot里面提供了一个更便捷的方式,非常适合 开发的时候调试接口使用, 个人比较喜欢O(∩_∩)O~

       4. 
@SpringBootApplication
 Spring注解
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

@EnableAutoConfiguration

@ComponentScan

的集合,功能集大成者~

main()方法中直接使用Spring boot的SpringApplication.run()方法就可以直接运行整个工程 了。

      5. 【测试】

运行工程,三种方法

1. 打成jar,命令行运行 java -jar jar名字

2. 打成war,发布到服务器

3. Spring boot

这里使用Spring boot,main()方法直接运行。

发布之后,可访问如下,

你可能感兴趣的文章
Unix + OS IBM Aix System Director
查看>>
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
framework apache commons
查看>>
my read work
查看>>
blancerServer IBM WebSphere Edge Server 6.1
查看>>
db db2 base / instance database tablespace container
查看>>
my read _job
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
searchServer IBM OminiFind / WebSphere Commerce SOLR
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
my read_Country
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
project bbs_discuz
查看>>