Spring引导请求
我在 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
- postman
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过 http://localhost:8080/client/get ?您的 server.servlet.context-path 属性可能不适用。 404 响应状态表明您尝试的 URL 不正确。
对于初学者来说,让它尽可能简单,然后开始增加复杂性。
达到了你的最终目标。这使得更容易检测您所犯的错误。您采取的步骤越简单,事情就越容易:)
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.
reached your final goal. This makes it much easier to detect errors you made. The simpler the steps you take the easier it gets :)