1. 程式人生 > >使用Spring Boot快速開發模式開發簡單的服務API

使用Spring Boot快速開發模式開發簡單的服務API

一、從start.spring.io下載相應的.zip檔案。


二、解壓.zip檔案並將相應內容匯入到eclipse或myeclipse中,pom.xml檔案如下。

<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>com.xzw</groupId>
  <artifactId>studysb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>studysb</name>
  <url>http://maven.apache.org</url>

 <parent>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.10.RELEASE</version>
	<relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-test</artifactId>
	  <scope>test</scope>
	</dependency>
	<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
  
   <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

三、編寫程式碼實現簡單的API

程式碼如下:

package com.xzw.springboot;

import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/xzw")
@EnableAutoConfiguration
public class StudySpringBoot02 {
	@RequestMapping("/name")
	public String index(){
		return "I am xzw";
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping("/date")
	String nowDate(){
		return "現在的時間是:" + (new Date()).toLocaleString();
	}
	
	@GetMapping("/person/{name}/{height}/{address}")
	public String getInfo(@PathVariable String name, @PathVariable Double height, @PathVariable String address){
		return "姓名:" + name + ",身高:" + height + ",家庭住址:" + address;
	}
	
	@GetMapping("/person/{name}-{height}-{address}")
	public String getInfo02(@PathVariable String name, @PathVariable Double height, @PathVariable String address){
		return "姓名:" + name + ",身高:" + height + ",家庭住址:" + address;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(StudySpringBoot02.class, args);
	}

}

執行結果如下:





至此,一個用SpringBoot開發的簡單的服務API就完成了。

你們在此過程中還遇到了什麼問題,歡迎留言,讓我看看你們都遇到了哪些問題。