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服務的難易程度。
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并對其進行配置。 讓我們開始吧。
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,我們可以使用它通過命令行輕松地創建此項目。這是完整的URL,您可以在瀏覽器中轉到該URL并下載到目前為止我們創建的骨架Maven項目。
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 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:
請正確地完成上述課程,這是本教程最重要的部分。 很容易理解這里發生了什么:
@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服務已經可以運行和測試了。
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常用注解、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方法調用的輸入。
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完成了所有配置,我們只需要專注于業務邏輯。 您可以從下面的鏈接下載最終項目。
翻譯自: https://www.journaldev.com/21127/spring-boot-rest-example
spring boot示例
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态