Spring Boot 构建restful web服务

1.创建依赖文件

文件名:pom.xml

文件内容:

<?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>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.1.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.huinongtx</groupId>

<artifactId>springboot3</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>springboot3</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

2.创建实体类

包名:com.huinongtx.entity

类名:Greeting

类内容:

public class Greeting {

    private Long id;

    private String name;


    // 省略getter and setter

}


包名:com.huinongtx.entity

类名:Quote

类内容:

public class Quote {

    private String type;

    private Value value;


    // 省略getter and setter

    }


    包名:com.huinongtx.entity

    类名:Value

    类内容:

    @JsonIgnoreProperties(ignoreUnknown = true)

public class Value {

    private Long id;

    private String quote;


    // 省略getter and setter

}

3.创建控制器

包名:com.huinongtx.controller

类名:GreetingController

类内容:

package com.huinongtx.controller;

import com.huinongtx.entity.Greeting;

import com.huinongtx.entity.Quote;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**

* Created by dengdashuai on 2018/12/11.

*/

@RestController

public class GreetingController {

    private static final String TEMPLATE = "hello,%s!";

    private final AtomicLong counter = new AtomicLong();

    private final RestTemplate restTemplate;


    @Autowired

    public GreetingController(RestTemplate restTemplate) {

        this.restTemplate = restTemplate;

    }


    @RequestMapping(value = "/getgreetobj1", method = GET)

    public Greeting getGreetObj1(@RequestParam(value = "name",defaultValue = "world") String name){

        return new Greeting(counter.incrementAndGet(),String.format(TEMPLATE,name));

    }


    @RequestMapping(value = "/getgreetobj2/{name}", method = GET)

    public Greeting getGreetObj2(@PathVariable(value = "name") String name) {

        return new Greeting(counter.incrementAndGet(), String.format(TEMPLATE, name));

    }


    @RequestMapping(value = "/client1", method = GET)

    public Quote client1() {

        RestTemplate innerRestTemplate = new RestTemplate();

        return innerRestTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);

    }


    @RequestMapping(value = "/client2", method = GET)

    public Quote client2() {

        return restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);

    }

}

4.应用入口文件

包名:com.huinongtx

类名:Springboot3Application

类内容:

package com.huinongtx;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.client.RestTemplateBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

@SpringBootApplication

public class Springboot3Application {

public static void main(String[] args) {

SpringApplication.run(Springboot3Application.class, args);

}

@Bean

public RestTemplate restTemplate(RestTemplateBuilder builder){

return builder.build();

}

}

5.启动服务并访问

1.png

http://localhost:8080/getgreetobj1

2.png

http://localhost:8080/getgreetobj1?name=diaochan

3.png

http://localhost:8080/getgreetobj2/lvbu

4.png

http://localhost:8080/client1

5.png

http://localhost:8080/client2

6.png

QQ截图20181211115228.png


猜你喜欢

转载自blog.51cto.com/suyanzhu/2328784