Spring Boot创建一个RESTful Web Service

简述

这篇文章简述了使用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;

@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));
    }
}

Make the application executable

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);
    }
}

@SpringBootApplication是一个便利的注解,它添加了以下所有:

  1. @configuration 标记的class作为一个bean资源在应用环境中定义。
  2. @EnableAutoConfiguration告诉Spring Boot 根据classpath配置、其它beans和各种属性配置去添加beans。
  3. 通常你会在一个Spring MVC应用中使用@EnableWebMvc,但是Spring Boot 在classpath中看到spring-webmvc时会自动使用该注解。
  4. @ComponentScan告诉Spring在当前包中查询其它的部件、配置和服务,并允许它去找到controllers.

在main()方法中调用了SpringApplication.run()方法去开始一个应用。

执行了以上操作后,你就可以访问:
http://localhost:8080/greeting
或者指定name值:
http://localhost:8080/greeting?name=zhangsan

返回的是一个json格式的字符串:
{"id":3,"name":"Hello, zhangsan!"}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值