【问题标题】:spring boot request mapping not showing dataspring boot 请求映射不显示数据
【发布时间】: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 boot crud 操作是如何工作的,需要了解 spring jpa 内置函数是如何工作的......
  • 谢谢!!! :D哈哈哈

标签: java spring-boot mapping


【解决方案1】:

控制器:

@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"}]

【讨论】:

    猜你喜欢
    • 2019-11-25
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2019-02-03
    • 2019-11-13
    • 2020-04-19
    • 2017-02-21
    相关资源
    最近更新 更多