简体   繁体   English

需要帮助了解 Spring Boot REST 接口

[英]Need help understanding Spring Boot REST interface

I am writing a simple Java Spring Boot test application to call a REST API.我正在编写一个简单的 Java Spring Boot 测试应用程序来调用 REST API。 The main purpose at this point is to quickly get something working and to also help refresh my memory/knowledge on making REST calls from a Java application.此时的主要目的是快速让某些东西工作,并帮助刷新我从 Java 应用程序进行 REST 调用的记忆/知识。

I've worked with Spring Boot in the past, as well as calling REST APIs, but it's been a few years and I no longer have access to the applications I had a hand in writing.我过去曾使用过 Spring Boot,也曾调用过 REST API,但已经有几年了,我不再能够访问我亲手编写的应用程序。 I've forgotten a lot of the details.我忘记了很多细节。

Here is what I have so far:这是我到目前为止所拥有的:

Application.java:应用程序.java:

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

TestController.java:测试控制器.java:

@RestController
public class TestController {
  @Autowired
  private RestTemplate template;

  @GetMapping("/groups")
  public Object[] getAllGroups(String url) {
    Object[] groups = template.getForObject(url, Object[].class);
    return groups;
  }
}

I'm not even at the point of trying to run this.我什至还没有尝试运行它。 Many of the online examples I've found leave out the details of how a URL is "provided" to the REST calls.我发现的许多在线示例都忽略了如何将 URL “提供”给 REST 调用的详细信息。

I also don't understand how the resource path /groups in @GetMapping is mapped to the REST call when the RestTemplate object is used in the method.我也不明白在方法中使用RestTemplate对象时,@ @GetMapping中的资源路径/groups是如何映射到 REST 调用的。 What is the purpose of the resource path in the annotation?注解中资源路径的作用是什么? How does it "fit" with the template call?它如何“适合”模板调用?

I'm assuming that the template should probably be used in a @Service , rather than in the controller, but I'm just trying to get this working and understand how it works.我假设模板可能应该在@Service中使用,而不是在控制器中,但我只是想让它工作并了解它是如何工作的。

On a related note, what other techniques are there to make the REST API URL available to the application besides passing it in to the RestTemplate object?在相关说明中,除了将 REST API URL 传递给RestTemplate对象之外,还有哪些其他技术可以使应用程序可以使用 REST API URL? Can this be done in some sort of configuration file?这可以在某种配置文件中完成吗? I've forgotten so much and am having a hard time finding an example that covers the finer details that are usually glossed over, if not completely left out.我已经忘记了这么多,并且很难找到一个涵盖通常被掩盖的更精细细节的示例,如果没有完全忽略的话。

You're misunderstanding the purpose of a @RestController vs RestTemplate .您误解了@RestController vs RestTemplate的目的。

Annotating a class with @RestController and @GetMapping annotations provides a REST interface which can service HTTP requests.使用@RestController@GetMapping注释对类进行注释提供了一个可以服务HTTP 请求的REST 接口。

Inside the implementation of getAllGroups you would normally use a service or a repository to get data from a DB, and return it to the client who had made the HTTP request.getAllGroups的实现中,您通常会使用服务或存储库从数据库中获取数据,并将其返回给发出 HTTP 请求的客户端。

A RestTemplate instance is used to make HTTP requests to other REST interfaces, provided by other servers. RestTemplate实例用于向其他服务器提供的其他 REST 接口发出 HTTP 请求。 It wouldn't normally be used in the implementation of a @RestController , unless some of the information returned came from some other microservice, for example.它通常不会在@RestController的实现中使用,除非返回的某些信息来自其他一些微服务,例如。

The REST API URL is for the use of clients calling your REST API, not for use within your controller. REST API URL 供调用您的 REST API 的客户端使用,而不是在您的控制器中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM