Spring引导请求

发布于 2025-01-20 19:59:47 字数 3998 浏览 0 评论 0原文

我在 java 中使用 Spring 编写了简单的应用程序(API-REST),问题是,当运行应用程序时,我在邮递员中请求获取所有客户端,根本不起作用,但应用程序与数据库有连接

  • 代码:

    • 客户端类

      <前><代码>@Entity @Table(名称=“客户”) 公共类客户端{ @ID @Column(名称=“id”) 私有整数 ID; @Column(名称=“名称”) 私有字符串名称; @Column(名称 = "电子邮件") 私人字符串电子邮件; @Column(姓名=“电话”) 私人国际电话; 公共客户端(){ } 公共客户端(字符串名称,字符串电子邮件,int 电话){ this.name = 名称; this.email = 电子邮件; this.phone = 电话; } 公共整数 getId() { 返回ID; } 公共无效setId(整数id){ 这个.id = id; } 公共字符串 getName() { 返回名称; } 公共无效setName(字符串名称){ this.name = 名称; } 公共字符串 getEmail() { 返回电子邮件; } 公共无效setEmail(字符串电子邮件){ this.email = 电子邮件; } 公共 int getPhone() { 返回电话; } 公共无效setPhone(int电话){ this.phone = 电话; }

      }

    • ClientController 类

      <前><代码> @RestController @RequestMapping(“/客户端”) 公共类 ClientController { @Autowired 私有 ClientService 客户端服务; @PostMapping("/保存") 公共可选<客户端>保存(@RequestBody客户端客户端){ 返回 clientService.save(client); } @GetMapping("/getById/{id}") 公共可选<客户端> findById(@PathVariable("id") 整数 id){ 返回 clientService.findById(id); } @DeleteMapping("/delete/{id}") 公共无效删除(@PathVariable(“id”)整数id){ clientService.delete(id); } @GetMapping("/get") 公共列表<客户端>查找全部(){ 返回 clientService.findAll(); }

      }

  • application.properties

     server.servlet.context-path=/company/api
     服务器端口=8080
    
     spring.datasource.url=jdbc:postgresql://localhost:5432/company
     spring.datasource.username=root
     spring.datasource.password=root
    
  • pom.xml

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       https://maven.apache.org/xsd/maven- 
       4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • 控制台

在此处输入图像描述

  • 邮递员

在此处输入图像描述

I have the application (API-REST) simple in java with spring, the problem is that when run the application and i do request in postman to get all clients, simply not work, but the application yes have connection with the database

  • Code:

    • Client class

      @Entity
      @Table(name = "clients")
      public class Client {
      
       @Id
       @Column(name = "id")
       private Integer id;
       @Column(name = "name")
       private String name;
       @Column(name = "email")
       private String email;
       @Column(name = "phone")
       private int phone;
      
       public Client() {
       }
      
       public Client(String name, String email, int phone) {
       this.name = name;
       this.email = email;
       this.phone = phone;
       }
      
       public Integer getId() {
       return id;
       }
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getEmail() {
        return email;
       }
      
       public void setEmail(String email) {
       this.email = email;
      }
      
      public int getPhone() {
      return phone;
      }
      
      public void setPhone(int phone) {
      this.phone = phone;
      }
      

      }

    • ClientController class

        @RestController
        @RequestMapping("/client")
        public class ClientController {
      
        @Autowired
        private ClientService clientService;
      
        @PostMapping("/save")
        public Optional<Client> save(@RequestBody Client client){
           return clientService.save(client);
        }
      
        @GetMapping("/getById/{id}")
        public Optional<Client> findById(@PathVariable("id") Integer id){
            return clientService.findById(id);
        }
      
        @DeleteMapping("/delete/{id}")
        public void delete(@PathVariable("id") Integer id){
           clientService.delete(id);
        }
      
        @GetMapping("/get")
        public List<Client> findAll(){
           return clientService.findAll();
      }
      

      }

  • application.properties

     server.servlet.context-path=/company/api
     server.port=8080
    
     spring.datasource.url=jdbc:postgresql://localhost:5432/company
     spring.datasource.username=root
     spring.datasource.password=root
    
  • pom.xml

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       https://maven.apache.org/xsd/maven- 
       4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • console

enter image description here

  • postman

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-01-27 19:59:47

您是否尝试过 http://localhost:8080/client/get ?您的 server.servlet.context-path 属性可能不适用。 404 响应状态表明您尝试的 URL 不正确。

对于初学者来说,让它尽可能简单,然后开始增加复杂性。

  1. 尝试通过删除 @RequestMapping("/client") 并删除 server.servlet.context-path=/company/api 使其适用于 http://localhost:8080/test 。
  2. 在此“/test”请求中仅返回字符串“Hello World”。
  3. 如果这有效,您可以逐步增加复杂性,直到您
    达到了你的最终目标。这使得更容易检测您所犯的错误。您采取的步骤越简单,事情就越容易:)

Have you tried http://localhost:8080/client/get ? Your property of server.servlet.context-path might not be applied. The response status of 404 indicates that the URL you are trying is not correct.

Also for starters make it as simple as possible and start adding complexity from there on.

  1. Try making it work for http://localhost:8080/test by removing @RequestMapping("/client") and removing server.servlet.context-path=/company/api.
  2. In this "/test" request just return a string "Hello World".
  3. If this works you can go step by step increasing the complexity until you
    reached your final goal. This makes it much easier to detect errors you made. The simpler the steps you take the easier it gets :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文