java - Spring Boot 应用程序已启动,但 POST 请求始终返回 "not found"

标签 java maven spring-boot

我的 spring-boot 应用程序正在运行,但每次我尝试执行 Get 请求时,返回的结果都是“未找到”,我不知道为什么我指定的路径无法识别,以下是相关的类(class):

英雄实体类:

@Entity
@Table(name="hero")
public class Hero
{
    @Id private int hid;//This is the table's primary key
    private PGpolygon area;
    private String spower;
    private String fname;
    private String lname;
    private DBApp.PowerCatagory pc;
    private float power_level;
    private float luck;
}

HeroRepository 类:

public interface HeroRepository extends CrudRepository<Hero, Integer>
{


}

HeroService 类

package services;
import entities.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import repositories.HeroRepository;


import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HeroService
{
    @Autowired//Injects the dependency
    private HeroRepository heroRepository;


    public List<Hero> getAllHeroes()
    {
        List<Hero> res = new ArrayList<>();
        heroRepository.findAll().forEach(res::add);
        return res;
    }

    public void addHero(Hero hero)
    {
        heroRepository.save(hero);
    }

    public Hero getHero(int id)
    {
        return heroRepository.findById(id).get();
    }

    public void updateHero(Hero hero)
    {
        heroRepository.save(hero);/*If a hero with the same id
        already exists in the DB then the save() function
        will automatically update that same tuple.*/
    }

    public void deleteHero(Hero hero)
    {
        heroRepository.delete(hero);
    }
}

HeroController 类:

package controllers;

import entities.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import services.HeroService;

import java.util.List;

@RestController
public class HeroController
{
    @Autowired
    private HeroService heroService;

    @GetMapping("/hero")
    public List<Hero> getAllHeroes()
    {
        //System.out.println(heroService.getAllHeroes());
        return heroService.getAllHeroes();
    }

    @GetMapping(value = "/test")
    public String test()
    {
        //System.out.println(heroService.getAllHeroes());
        return "working!!!";
    }

}

和我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MyDBExcercise</groupId>
    <artifactId>mydbexxcercise</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>spring-data-aerospike</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.2.Final</version>
        </dependency>
    </dependencies>


</project>

我真的不知道为什么我指定的路径无法识别,因此我们将非常感谢您的帮助,非常感谢!

更新: 感谢@Atul K,我已经取得了一些进展,现在我遇到了 上下文初始化期间遇到以下错误异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建文件 [D:\Projects\Java\mydbexxcercise\target\classes 中定义的名为“heroController”的 bean 时出错\com\db\controllers\HeroController.class]:bean 实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.db.controllers.HeroController]:构造函数抛出异常;嵌套异常是java.lang.NullPointerException。 这些是更新的类:

HeroRepository 类:

@Repository
public interface HeroRepository extends JpaRepository<Hero, Integer>
{


}

请注意,接口(interface)现在扩展了 JpaRepository 而不是像上次一样的 CrudRepository(在查看控制台后,我注意到 Spring boot 没有设法找到任何存储库,因此发生了更改)。 HeroService 类:

package com.db.services;
import com.db.entities.Hero;
import com.db.repositories.HeroRepository;
import com.db.app.DBApp;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HeroService
{
    private HeroRepository heroRepository = DBApp.getAppContext().getBean(HeroRepository.class);

    public List<Hero> getAllHeroes()
    {
        List<Hero> res = new ArrayList<>();
        heroRepository.findAll().forEach(res::add);
        return res;
    }

    public void addHero(Hero hero)
    {
        heroRepository.save(hero);
    }

    public Hero getHero(int id)
    {
        return heroRepository.findById(id).get();
    }

    public void updateHero(Hero hero)
    {
        heroRepository.save(hero);/*If a hero with the same id
        already exists in the DB then the save() function
        will automatically update that same tuple.*/
    }

    public void deleteHero(Hero hero)
    {
        heroRepository.delete(hero);
    }
}

HeroController 类:

package com.db.controllers;
import com.db.app.DBApp;
import com.db.entities.Hero;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.db.services.HeroService;

import java.util.List;

@RestController
public class HeroController
{
    private HeroService heroService = DBApp.getAppContext().getBean(HeroService.class);

    @GetMapping("/hero")
    public List<Hero> getAllHeroes()
    {
        //System.out.println(heroService.getAllHeroes());
        return heroService.getAllHeroes();
    }

    @GetMapping("/")
    public String test()
    {
        //System.out.println(heroService.getAllHeroes());
        return "working!!!";
   }

}

根据控制台显示,Spring无法初始化HeroController的原因是以下代码引发的异常:

    private HeroService heroService = DBApp.getAppContext().getBean(HeroService.class);

我不知道为什么这里会抛出异常,所以再次,我们将非常感谢任何帮助。

最佳答案

我只在你的 RestController 中看到 @GetMapping 。如果您想使用 Post,请使用 @PostMapping

如果您的目标只是获取数据列表,请使用 GET 请求,因为这是标准约定。

关于java - Spring Boot 应用程序已启动,但 POST 请求始终返回 "not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124604/

相关文章:

java - 如何使用map函数get

java - 使用 boolean 值在 Java 中实现素数筛

java - java spring中的注释@Repository如何工作?

java - 休息模板 HttpClientErrorException : 400 null on Post API

spring-boot - spring-boot 之外的 ConfigurationProperties

java - 在 CXF Spring Boot 中注册过滤器

maven - maven-resources-plugin:2.5-无法创建资源输出目录

Spring Roo 说 "Version is required for org.apache.maven.plugins:maven-compiler-plugin"

Java Maven OSGi 从文件系统动态加载 jar 并在运行时从中运行类方法

java - Autowiring ,不按类型装配 bean