springboot配置文件,spring boot示例_Spring Boot REST示例

 2023-11-19 阅读 34 评论 0

摘要:spring boot示例Spring Boot is an awesome module from Spring Framework. Once you are used to it, then working with Spring is a breeze because it takes care of all the spring container specific configurations and allows us to focus more on our application.

spring boot示例

Spring Boot is an awesome module from Spring Framework. Once you are used to it, then working with Spring is a breeze because it takes care of all the spring container specific configurations and allows us to focus more on our application.

Spring Boot是Spring Framework的一個很棒的模塊。 一旦習慣了,那么使用Spring是一件輕而易舉的事,因為它可以處理所有特定于Spring容器的配置,并使我們可以將更多精力放在應用程序上。

springboot配置文件。REST web services are very popular these days, so in this tutorial, we will see how easily we can create a RESTful web service using Spring Boot.

REST Web服務如今非常流行,因此在本教程中,我們將看到使用Spring Boot創建RESTful Web服務的難易程度。

Spring Boot REST依賴關系 (Spring Boot REST Dependencies)

Since web services run on the server, apart from Spring Boot we need to add Spring MVC to our project. I am using Eclipse to create my basic spring boot project, but you can also use Spring Initializr.

由于Web服務在服務器上運行,因此除了Spring Boot之外,我們需要將Spring MVC添加到我們的項目中。 我正在使用Eclipse創建基本的Spring Boot項目,但是您也可以使用Spring Initializr 。

Spring data rest、When we use Eclipse for creating Spring project, it actually uses Spring Initializr and configures it. Let’s get started.

當我們使用Eclipse創建Spring項目時,它實際上使用Spring Initializr并對其進行配置。 讓我們開始吧。

Spring Boot REST項目 (Spring Boot REST Project)

  1. Create new Spring Starter Project as shown in below image. Eclipse should have Spring support for this, all the latest Eclipse release has built-in Spring support.
    new spring starter project

    如下圖所示,創建新的Spring Starter Project。 Eclipse應該對此具有Spring支持,所有最新的Eclipse版本都具有內置的Spring支持。
  2. In the next popup screen, provide project basic details. I am using Maven, you can use Gradle too.
  3. Next screen asks to add dependencies apart from Spring Boot. Add “Web” dependencies and click next.
    spring boot rest maven dependencies

    下一個屏幕要求添加除Spring Boot以外的依賴項。 添加“ Web”依賴項,然后單擊“下一步”。
  4. We could have click on Finish in the earlier screen, but clicking on next provides us the Spring Initializr URL that we can use to create this project easily through the command line.
    spring initializr project url

    Here is the complete URL, you can just go to this URL in the browser and download the skeleton maven project that we have created so far.

    我們可以在前面的屏幕中單擊“完成”,但是單擊下一步將為我們提供Spring Initializr URL,我們可以使用它通過命令行輕松地創建此項目。
    Spring Initializr項目網址

    這是完整的URL,您可以在瀏覽器中轉到該URL并下載到目前為止我們創建的骨架Maven項目。

  5. Below image shows the contents of the auto-generated project.

    java springboot?Most important of these is SpringBootRestApplication class that is configured as Spring Boot application and contains java main method. When we run this class, it automatically runs our project as Spring Boot Web project.

    If you will check the console logs, it should print Tomcat started on port(s): 8080 (http) with context path ''.

    其中最重要的是配置為Spring Boot應用程序并包含java main方法的SpringBootRestApplication類。 當我們運行此類時,它會自動將我們的項目作為Spring Boot Web項目運行。

    如果您將檢查控制臺日志,則應打印Tomcat started on port(s): 8080 (http) with context path ''

springboot2。Now that our base Spring Boot REST application is ready, it’s time to add some endpoints and business logic to it.

現在我們的基本Spring Boot REST應用程序已經準備就緒,是時候添加一些端點和業務邏輯了。

We will use a java bean class for sending REST web service response. Here is the code for it.

我們將使用Java bean類來發送REST Web服務響應。 這是它的代碼。

package com.journaldev.spring;import org.springframework.stereotype.Component;@Component
public class Person {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}

Spring REST端點配置 (Spring REST Endpoints Configuration)

Spring boot。We will create a @RestController and add some methods and annotate them with @RequestMapping. Below is our REST Controller class that exposes few GET and POST endpoints.

我們將創建一個@RestController并添加一些方法,并使用@RequestMapping對其進行注釋。 下面是我們的REST Controller類,它公開了一些GET和POST端點。

package com.journaldev.spring;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class PersonController {@Autowiredprivate Person person;@RequestMapping("/")public String healthCheck() {return "OK";}@RequestMapping("/person/get")public Person getPerson(@RequestParam(name="name", required=false, defaultValue="Unknown") String name) {person.setName(name);return person;}@RequestMapping(value="/person/update", method=RequestMethod.POST)public Person updatePerson(@RequestParam(name="name", required=true) String name) {person.setName(name);return person;}}

Please go through the above class properly, this is the most important part of this tutorial. It’s very easy to understand what is happening here:

請正確地完成上述課程,這是本教程最重要的部分。 很容易理解這里發生了什么:

  • When we annotate the controller class with @RestController, Spring Boot scans it to find the REST endpoints to configure.

    當我們使用@RestController注釋控制器類時,Spring Boot會對其進行掃描以查找要配置的REST端點。
  • @RequestMapping annotation on methods tells Spring to use this method as handler for any client requests. This is very important spring annotation, we can configure additional features such as HTTP methods supported, Content-Type of request, response content type etc. By default, GET method is supported and JSON response is returned.

    方法上的@RequestMapping注釋告訴Spring將這個方法用作任何客戶端請求的處理程序。 這是非常重要的spring注釋,我們可以配置其他功能,例如支持的HTTP方法,請求的Content-Type,響應內容類型等。默認情況下,支持GET方法并返回JSON響應。

springboot中文手冊、That’s it, our Spring Boot RESTful web service is ready to run and test.

就是這樣,我們的Spring Boot RESTful Web服務已經可以運行和測試了。

Spring Boot REST Web服務測試 (Spring Boot REST Web Service Test)

When you will run the main class, check the logs for endpoints configuration.

當您運行主類時,請檢查日志以了解端點配置。

2018-05-25 16:20:15.961  INFO 11802 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.journaldev.spring.PersonController.healthCheck()
2018-05-25 16:20:15.962  INFO 11802 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/get]}" onto public com.journaldev.spring.Person com.journaldev.spring.PersonController.getPerson(java.lang.String)
2018-05-25 16:20:15.962  INFO 11802 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/update],methods=[POST]}" onto public com.journaldev.spring.Person com.journaldev.spring.PersonController.updatePerson(java.lang.String)

spring boot注解詳解,All seems good, GET requests can be tested in the browser itself.

一切似乎都不錯,可以在瀏覽器本身中測試GET請求。

I am using Postman Chrome app for testing POST requests.

我正在使用Postman Chrome應用測試POST請求。

Spring Boot REST JSON請求響應 (Spring Boot REST JSON Request Response)

spring boot常用注解、JSON is the standard messaging format for REST web services, I didn’t liked that we are accepting String as POST request, let’s change our method to accept JSON request.

JSON是REST Web服務的標準消息傳遞格式,我不喜歡我們接受String作為POST請求,讓我們將方法更改為接受JSON請求。

@RequestMapping(value="/person/update", method=RequestMethod.POST, consumes = "application/json")public Person updatePerson(@RequestBody Person p) {person.setName(p.getName());return person;
}

That’s it, couple of changes in method annotations and we are done. Now when you will run the main program, it will produce following output for endpoint configuration.

就這樣,方法注釋中的幾個更改就完成了。 現在,當您運行主程序時,它將為端點配置生成以下輸出。

2018-05-25 16:31:12.613  INFO 11813 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/update],methods=[POST],consumes=[application/json]}" onto public com.journaldev.spring.Person com.journaldev.spring.PersonController.updatePerson(com.journaldev.spring.Person)

Below image shows the Postman settings and input for the POST method call.

下圖顯示了Postman設置和POST方法調用的輸入。

摘要 (Summary)

In this post, we learned how to use Spring Boot to create a RESTful web service. Spring Boot did all the configurations and we only had to focus on our business logic. You can download the final project from below link.

在本文中,我們學習了如何使用Spring Boot創建RESTful Web服務。 Spring Boot完成了所有配置,我們只需要專注于業務邏輯。 您可以從下面的鏈接下載最終項目。

Spring Boot REST Project.Spring Boot REST項目。
GitHub Repository.GitHub Repository下載該項目。

翻譯自: https://www.journaldev.com/21127/spring-boot-rest-example

spring boot示例

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/182957.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息