使用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就完成了。


你们在此过程中还遇到了什么问题,欢迎留言,让我看看你们都遇到了哪些问题。

猜你喜欢

转载自blog.csdn.net/gdkyxy2013/article/details/79487587