webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)

一、从start.spring.io下载相应的.zip文件。

webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)(1)

二、解压.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>

三、编写代码实现简单的WebService服务的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); } }

运行结果如下:

webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)(2)

webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)(3)

webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)(4)

webservice springboot区别(Boot快速开发模式开发简单的WebService服务API)(5)

至此,一个用SpringBoot开发的简单WebService服务的API就完成了。

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页