spring_使用Spring Boot开发简单服务

spring

spring

在本文中,我将演示如何使用Spring Boot创建一个简单的Web服务。 只要有适当的依赖关系,此框架几乎就可以轻松开发Web服务。 在此示例中,我将创建一个Web服务,该服务将从文件中读取当前温度,并通过RESTful端点将其提供给客户端。

Spring Initializr帮助人们选择生产特定解决方案所需的依赖项。 可以在以下位置找到Initializr: https//start.spring.io/

使用Initializr创建项目时要注意的第一件事是,可以使用多种不同的JVM语言(包括Java,Kotlin或Groovy)开发Spring Boot应用程序。 这是Spring Boot与Jakarta EE有所不同的地方,Jakarta EE主要关注Java语言。 还可以选择为自己的Spring Boot项目选择Gradle或Maven。 使用Initialzr创建项目中最繁琐的部分之一可能是为项目选择适当的依赖项。 原因是有太多的依赖选项,所以花一些时间才能了解哪些对特定解决方案最有用。 这不是一件坏事……只是随着时间的推移需要学习的东西。

首先,请在Initializr中选择以下选项:

项目:Maven

语言:Java

Sprint Boot:2.3.3

依赖关系:Spring Web

项目元数据

–组:org.demo

–神器:poolservice

–名称:poolservice

–软件包名称:org.demo.poolservice

–包装:战争

– Java:11

选择这些选项后,单击“生成”以下载项目文件。 下载后,在您喜欢的IDE中打开项目。 在这种情况下,我将使用Apache NetBeans12。现在可以构建该项目并将其部署到诸如Payara服务器的容器中。 该项目已配置完毕,可以开始添加RESTful服务。 以下代码显示了由Initializr创建的,包含@SpringBootApplication批注的生成的PoolserviceApplication类。

 package org.demo.poolservice;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
 public class PoolserviceApplication { 
    public static void main(String[] args) {

        SpringApplication.run(PoolserviceApplication. class , args);

    }
 }

@SpringBootApplication批注是一种快捷方式批注,它结合了以下三个批注的默认功能:

  • @EnableAutoConfiguration:启用Spring Boot自动配置
  • @ComponentScan:在加载应用程序的软件包上启用组件扫描
  • @Configuration:允许在上下文中注册额外的bean或导入其他配置类的能力

由于应用程序是默认配置的,因此可以通过在同一程序包中创建一个名为HelloController的类并将以下代码放入其中来生成新的RESTful服务:

 package org.demo.poolservice;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RequestMapping;
 @RestController
 public class HelloController { 
    @RequestMapping ( "/" )

    public String index() {

        return "Greetings from Spring Boot This is the main service!" ;

    }
 }

@RestController批注通过结合传统的@Controller和@ResponseBody批注功能将类连接为RESTful Web服务控制器。 在此示例中,正如@RequestMapping批注所示,我使用请求根来提供响应。 一旦添加了该控制器类并且已编译并重新部署了代码,就可以访问URL http:// localhost:8080 / poolservice以显示消息:“ Spring Boot的问候,这是主要服务!”。

现在该通过创建一个名为TemperatureReader的新类来添加从文本文件(由Raspberry Pi编写)中读取当前温度的功能了。 通过使用@Component注释该类并指定将引用该bean的名称,可以使该类成为上下文bean。 在这种情况下,为“ temperatureReader”。 该类的功能非常简单,因为它从格式为(23.5,78.3)的文件中读取温度,并使它们分别可以通过currentTemperatureCcurrentTemperatureF进行访问。

 package org.demo.poolservice;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import org.springframework.stereotype.Component;  @Component ( "temperatureReader" )
 public class TemperatureReader {
    
    private String currentTemperatureF;
    
    private String currentTemperatureC;
    
    protected String readTemperatureFile() {
                
        String temperatureFile = "/<>/temperature.txt" ;

        System.out.println( "Temperature File: " + temperatureFile);

        java.nio.file.Path path = Paths.get(temperatureFile);

        String currentTemperature = null ;

        try (BufferedReader reader = Files.newBufferedReader(path, Charset.forName( "UTF-8" ))) { 
            String currentLine = null ;

            while ((currentLine = reader.readLine()) != null ) { //while there is content on the current line

                currentTemperature = currentLine;

            }

        } catch (IOException ex) {

            ex.printStackTrace(); //handle an exception here

        }

        return currentTemperature;

    }

    /**

     * @return the currentTemperatureF

     */

    public String getCurrentTemperatureF() {

        String temp = readTemperatureFile();

        currentTemperatureF = temp.substring(temp.indexOf( "," ) + 1 , temp.lastIndexOf( ")" ));

        return currentTemperatureF;

    }

    /**

     * @param currentTemperatureF the currentTemperatureF to set

     */

    public void setCurrentTemperatureF(String currentTemperatureF) {

        this .currentTemperatureF = currentTemperatureF;

    }

    /**

     * @return the currentTemperatureC

     */

    public String getCurrentTemperatureC() {

        String temp = readTemperatureFile();

        currentTemperatureC = temp.substring(temp.indexOf( "(" ) + 1 , temp.lastIndexOf( "," ));

        return currentTemperatureC;

    }

    /**

     * @param currentTemperatureC the currentTemperatureC to set

     */

    public void setCurrentTemperatureC(String currentTemperatureC) {

        this .currentTemperatureC = currentTemperatureC;

    }
    }

最后,要通过RESTFul服务提供温度读数,请创建一个名为TemperatureController的控制器,并使用@RestController对其进行注释。 通过使用@Autowired注释专用的TemperatureReader字段来注入TemperatureReader。 然后可以通过使用TemperatureReader字段将bean用于获取温度。 服务的URL映射是通过index()方法上的@RequestMapping(“ / temperature”)注释提供的,该注释将用于在各个请求映射处提高温度。 index()方法中包含的功能非常少,因为它仅调用temperatureReader.getCurrentTemperatureF()方法以以华氏温度返回当前温度。

 package org.demo.poolservice;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;  @RestController
 public class TemperatureController {
    
    @Autowired

    private TemperatureReader temperatureReader;
    
    @RequestMapping ( "/temperature" )

    public String index() {

            return temperatureReader.getCurrentTemperatureF();

    }
    }

要查看温度,请访问URL:http:// localhost:8080 / poolservice / temperature

翻译自: https://www.javacodegeeks.com/2020/09/developing-a-simple-service-with-spring-boot.html

spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值