个性化阅读
专注于IT技术分析

使用Spring Boot初始化RESTful Web服务项目

本文概述

步骤1:从https://spring.io/tools3/sts/all下载Spring Tool Suite(STS)并解压缩。

步骤2:启动STS。

第3步:点击文件菜单->新建-> Spring Starter项目->

使用Spring Boot初始化RESTful Web服务项目

如果未征募Spring Starter Project, 请单击菜单底部的“其他”。屏幕上出现一个对话框。在向导文本框中输入Spring Starter Project, 然后单击下一步按钮。

使用Spring Boot初始化RESTful Web服务项目

步骤4:提供项目的名称, 组和包。我们提供了:

名称:restful-web-services

组:com.srcmini

封装:com.srcmini.server.main

单击下一步按钮。

使用Spring Boot初始化RESTful Web服务项目

步骤5:选择Spring Boot版本2.1.8。

使用Spring Boot初始化RESTful Web服务项目

步骤6:我们可以在项目浏览器窗口中看到项目结构。

使用Spring Boot初始化RESTful Web服务项目

步骤7:转到Maven存储库https://mvnrepository.com/, 并在pom.xml中添加Spring Web MVC, Spring Boot DevTools, JPA和H2依赖项。添加依赖项后, pom.xml文件如下所示:

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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.srcmini</groupId>
<artifactId>restful-web-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restful-web-services</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>2.5</version>
</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>

步骤8:现在打开RestfulWebServicesApplication.java文件, 并以Java Application的身份运行该文件。

package com.srcmini.server.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestfulWebServicesApplication 
{
public static void main(String[] args) 
{
SpringApplication.run(RestfulWebServicesApplication.class, args);
}
}

它不执行任何服务, 但是确保应用程序正常运行。

输出量

使用Spring Boot初始化RESTful Web服务项目

创建一个Hello World服务

步骤1:在com.srcmini.server.main包中创建一个名为HelloWorldController的新类。

步骤2:无论何时创建Web服务, 我们都需要定义两个方法Get方法和URI。现在, 创建helloWorld()方法, 该方法返回字符串“ Hello World”。如果我们想告诉Spring MVC它将会处理REST请求, 我们必须添加@RestController注释。现在, 它成为可以处理Rest请求的Rest控制器。

我们要做的下一步是为该方法创建一个映射。在helloWorld()方法上方添加@RequestMapping批注。 HelloWorldController如下所示:

package com.srcmini.server.main;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
//Controller
@RestController
public class HelloWorldController 
{
//using get method and hello-world as URI
@RequestMapping(method=RequestMethod.GET, path="/hello-world")
public String helloWorld()
{
return "Hello World";
}
}

我们还可以通过使用@GetMapping批注而不是@RequestMapping来改进上述代码。这里不需要方法说明。

package com.srcmini.server.main;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//Controller
@RestController
public class HelloWorldController 
{
//using get method and hello-world as URI
@GetMapping(path="/hello-world")
public String helloWorld()
{
return "Hello World";
}
}

步骤3:运行RestfulWebServiceApplication。它在浏览器上显示字符串Hello World。

增强Hello World服务以返回Bean

在本节中, 我们将为helloWorld()方法生成一个bean。

步骤1:在HelloWordController.java文件中创建helloWorldBean()方法。将URI映射到“ / hello-world-bean”并返回HelloWorldBean。

HelloWorldController.java

package com.srcmini.server.main;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//Controller
@RestController
public class HelloWorldController 
{
//using get method and hello-world URI
@GetMapping(path="/hello-world")
public String helloWorld()
{
return "Hello World";
}
@GetMapping(path="/hello-world-bean")
public HelloWorldBean helloWorldBean()
{
return new HelloWorldBean("Hello World"); //constructor of HelloWorldBean
}
}

步骤2:创建一个类HelloWorldBean。

步骤3:生成Getter和Setter。

右键单击->源->生成Getter和Setters->选中框->确定

步骤4:产生toString()。

右键单击->源->生成toString()。->确定

HelloWorldBean.java

package com.srcmini.server.main;
public class HelloWorldBean 
{
public String message;
//constructor of HelloWorldBean
public HelloWorldBean(String message)
{
this.message=message;
}
//generating getters and setters
public String getMessage()
{
return message;
}
public void setMessage(String message) 
{
this.message = message;
}
@Override
//generate toString
public String toString() 
{
return String.format ("HelloWorldBean [message=%s]", message);
}
}

步骤5:启动HelloWorldController。浏览器的URL更改为localhost:8080 / hello-world-bean。

它以JSON格式返回消息“ Hello World”。

{
message: "Hello World"
}

赞(0)
未经允许不得转载:srcmini » 使用Spring Boot初始化RESTful Web服务项目

评论 抢沙发

评论前必须登录!