spring-guide之rest-service

本文介绍如何使用Spring Boot创建RESTful服务,通过示例展示如何搭建项目、编写控制器和服务接口,以及如何处理HTTP GET请求并返回JSON响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-guide之rest-service

目标:创建一个spring的符合restful规范的web service项目,并做出hello world。

构建一个简单的符合restful规范的web服务

将构建一个接受http get请求的web服务。

http://localhost:8080/greeting

并且返回一个json格式的响应

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

同样,可以在url请求上自定义name参数,用来覆盖 World的值。、

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

需要准备的

  1. 大概15分钟
  2. 一个友好的文本编辑器或者IDE
  3. jdk1.8 或者更晚的(我用的1.7)
  4. gradle2.3+ 或者maven3.0+ (我用的gradle)
  5. 也可以直接从github上导入代码

    官方:git clone https://github.com/spring-guides/gs-rest-service.git
    我的:https://github.com/chenlisong/projects/tree/master/gs-rest-service

gradle的一些配置

  1. gradle的快速学习

    官网:https://gradle.org/
    快速的使用gradle创建一个java项目:http://spring.io/guides/gs/gradle/

  2. 创建一个目录

    mkdir -p src/main/java/hello;
    
    └── src
        └── main
            └── java
                └── hello
  3. 创建gradle的配置文件build.gradle

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
        }
    }
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'spring-boot'
    
    jar {
        baseName = 'gs-rest-service'
        version =  '0.1.0'
    }
    
    repositories {
        mavenCentral()
    }
    
    //我在这里将jdk改成1.7,原值1.8
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web")
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
  4. 使用spring-boot启动web服务

    1. 使用方便简单
    2. 查找main方法来执行
    3. 解决依赖的jar包

代码实现

目标:通过项目和build系统,来创建一个简单的web服务。

服务将提供/greeting 类型是http get的接口,参数名是name,类型是string,用来替换world。

返回值如下

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

id是调用次数。

Greeting.java代码如下:

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;
    }
}
spring使用jackson json的jar包,自动把Greeting类解析成为json格式的字符串

创建一个Controller
src/main/java/hello/GreetingController.java

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;

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

再创建一个启动器,用spring-boot来启动。

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

构建成jar包的方式并执行

    //启动服务
    ./gradlew bootRun -info | debug 

    //build成为jar包,并放在build/libs下
    ./gradlew build

    //cmd下执行jar并启动服务
    java -jar build/libs/gs-rest-service-0.1.0.jar

测试一下

 request:http://localhost:8080/greeting

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

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

 response:{"id":2,"content":"Hello, User!"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值