java web 服务启动初始化类,使用Spring Boot初始化RESTful Web服务项目

本文概述

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

步骤2:启动STS。

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

restful-web-services-example.png

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

restful-web-services-example2.png

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

名称:restful-web-services

组:com.srcmini

封装:com.srcmini.server.main

单击下一步按钮。

restful-web-services-example3.png

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

restful-web-services-example4.png

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

restful-web-services-example5.png

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

pom.xml

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

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.8.RELEASE

com.srcmini

restful-web-services

0.0.1-SNAPSHOT

restful-web-services

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-activemq

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-tomcat

org.springframework

spring-webmvc

org.springframework.boot

spring-boot-devtools

runtime

org.hibernate.javax.persistence

hibernate-jpa-2.1-api

1.0.0.Final

com.h2database

h2

runtime

org.apache.maven

maven-archiver

2.5

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

步骤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);

}

}

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

输出量

restful-web-services-example6.png

创建一个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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值