Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a RESTful interface in Spring Boo

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to build a RESTful interface in Spring Boo, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

RESTful

I believe all of you here have heard a little about RESTful, so what on earth is RESTful?

REST (Representational State Transfer) declarative state transition is a set of architectural constraints and principles. The application or design that meets these constraints and principles is RESTful. It is important to note that REST is a design style rather than a standard. REST is usually based on the use of HTTP,URI, XML (a subset of the standard general markup language) and HTML (an application under the standard general markup language), which are widely used protocols and standards.

Maybe this passage is a little obscure. Let's explain RESTful from another angle.

First, let's take a look at a set of examples:

/ / query all personnel (traditional) localhost:8088/api/user/findAll request method: GET// query all personnel (RESTful) localhost:8088/api/users request method: GET// modifier (traditional) localhost:8088/api/user/update request method: POST// modifier (RESTful) localhost:8088/api/users request method: PUT// add personnel (traditional) localhost:8088/api/user/ Add request method: POST// add person (RESTful) localhost:8088/api/users request method: POST// delete person (traditional) localhost:8088/api/user/delete request method: DELETE// delete person (RESTful) localhost:8088/api/users request method: DELETE

We usually call the address entered in the address bar URI (Uniform Resource Identifier), which is translated into Chinese as the uniform resource identifier.

Resources, what we see on the browser page can be called resources, such as pictures, text, voice and so on.

URI is used to locate these resources, and only nouns representing resources appear in RESTful-style interfaces. The operation of this resource is distinguished by several request types built into HTTP. The same path localhost:8088/api/users, because of the different request methods, to find different interfaces to complete the transformation of the state of resources.

To sum up, REST refers to the different operations (query, add, change, delete) made under different GET,POST,PUT,DELETE (expressions) of the same URI resources, changing the state of the resources, that is, declarative state transfer. A URI that conforms to the REST style can be called a RESTful interface.

Seeing here, I believe RESTful already knows enough. Let's take a look at how to use it in Spring Boot.

SpringMVC builds RESTful Interface

SpringMVC provides us with some notes that can help us build the interface of RESTful. Let's take a look at the code:

Package indi.viyoung.viboot.restful.controller;import indi.viyoung.viboot.restful.entity.User;import indi.viyoung.viboot.restful.service.UserService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/** *

* Front-end controller *

* * @ author viyoung * @ since 2019-01-23 * / @ RestController@RequestMapping ("/ users") @ Slf4j@CrossOriginpublic class UserController {@ Autowired private UserService userService; @ GetMapping public List get () {log.info ("GET method execution.") ; return userService.list ();} @ GetMapping (value = "/ {id}") public User get (@ PathVariable String id) {log.info ("GET.. {}... method execution." , id); return userService.getById (id);} @ PostMapping public void post () {log.info ("POST method execution.") ;} @ PutMapping public void put () {log.info ("PUT method executes.") ;} @ DeleteMapping public void delete () {log.info ("DELETE method executes.") ;}} about how to build a RESTful interface in Spring Boo is shared here. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report