我已经运行了我的spring-boot应用程序,但是每次我尝试执行Get请求时,返回的结果都是“未找到”,并且我不知道为什么无法识别我指定的路径,以下是相关的类:
英雄实体类:
@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,我已经取得了一些进展,现在我遇到了
以下错误
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'heroController' defined in file [D:\Projects\Java\mydbexxcercise\target\classes\com\db\controllers\HeroController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.db.controllers.HeroController]: Constructor threw exception; nested exception is java.lang.NullPointerException
。这些是更新的类:
HeroRepository类:
@Repository
public interface HeroRepository extends JpaRepository<Hero, Integer>
{
}
请注意,该接口现在像上次一样扩展了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
。如果要使用后期处理,请使用@PostMapping
。
如果您的目标只是获取数据列表,请使用GET请求,因为它是标准约定。