【发布时间】:2022-01-12 16:07:07
【问题描述】:
为什么我没有数据?
用户控制器:
package example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import example.model.User;
import example.service.UserService;
@RequestMapping("/api/users")
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> findAll() {
return userService.findAll();
}
}
用户服务:
package example.service;
import java.util.List;
import example.model.User;
public interface UserService {
public List <User> findAll();
}
UserServiceImpl:
package example.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import example.model.User;
import example.service.UserService;
@Service
public class UserServiceImpl implements UserService{
private static List <User> usersList = new ArrayList<>();
private static Integer COUNTER = 1;
static {
User user = new User(COUNTER++, "Dusan", "Bosiljkic", 22, "Srbija");
usersList.add(user);
user = new User(COUNTER++, "Milan", "Stokic", 22, "Srbija");
usersList.add(user);
}
@Override
public List<User> findAll() {
return usersList;
}
}
主要:
package example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
pom 文件:
<?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.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CRUD App using Spring Boot</name>
<description>CRUD App using Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</project>
当我输入 localhost:8080/api/users/ 时,我得到了相同的白标签消息。 我尝试更改端口,但未使用 8080 感谢帮助 一些细节是我只是尝试使用 CRUD 选项制作网络应用程序,我什至无法迈出第一步。文字够了吗?
【问题讨论】:
-
你需要了解
spring bootcrud 操作是如何工作的,需要了解 spring jpa 内置函数是如何工作的...... -
谢谢!!! :D哈哈哈
标签: java spring-boot mapping