为什么我没有得到数据?
用户控制器:
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;
}
}
Main:
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/im时,会得到相同的白标签消息。我尝试更改端口,8080未使用谢谢你的帮助一些细节是,我只是试图用CRUD选项制作网络应用程序,但我甚至无法进入第一步。这够文本了吗
控制器:
@RestController
@RequestMapping("/api")
public class Controller {
@Autowired
private TestService testSrv;
@GetMapping(path="/users", produces="application/json")
public List<Person> findAll() {
return testSrv.findAll();
}
}
测试服务
@Service
public class TestService {
private static List <Person> usersList = new ArrayList<>();
private static Integer COUNTER = 1;
static {
Person user = new Person(COUNTER++, "Dusan", "Bosiljkic", 22, "Srbija");
usersList.add(user);
user = new Person(COUNTER++, "Milan", "Stokic", 22, "Srbija");
usersList.add(user);
}
public List<Person> findAll(){
return usersList;
}
}
人员(抱歉不是用户(
public class Person {
private Integer id;
private String firstName;
private String lastName;
private Integer age;
private String country;
public Person(Integer id, String firstName, String lastName, Integer age, String country) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.country = country;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
输出:http://localhost:8080/api/users
[{"id":1,"firstName":"Dusan","lastName":"Bosiljkic","age":22,"country":"Srbija"},{"id":2,"firstName":"Milan","lastName":"Stokic","age":22,"country":"Srbija"}]