我已经运行了我的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);
我不知道为什么在这里再次抛出异常,因此,我们将不胜感激。
答案 0 :(得分:2)
我在您的RestController中仅看到@GetMapping
。如果要使用Post,请使用@PostMapping
。
如果您的目标只是获取数据列表,请使用GET请求,因为它是标准约定。
答案 1 :(得分:0)
如果要拨打POST
,则需要在代码中替换以下行:
@GetMapping("/hero")
收件人:
@PostMapping("/hero")
仅此而已。另外,如果您想同时使用GET
和POST
,也可以使用以下代码。
@RequestMapping(value="/hero", method=RequestMethod.POST)
和:
@RequestMapping(value="/hero", method=RequestMethod.GET)
答案 2 :(得分:0)
尝试在HeroController上方使用@RequestMapping(“ / api”)并使用“ / api / hero”作为GET请求访问该方法
答案 3 :(得分:0)
由于类的打包,Spring无法扫描您使用过的注释。
尽管上面的代码片段中没有提到Spring Boot Application类的软件包,所以我们假设它是com.example
。然后使用以下3个选项可以解决此问题:
选项1 :(建议),然后将controllers
前面加上services
,将其com.example
,com.example.controllers
等包重命名为{{1} }等。
OR
选项2(如果不想更改包装):
将以下注释与@SpringBootApplication
一起添加到Spring Boot应用程序类中:
@ComponentScan(basePackages = { "controllers", "services" })
@EnableJpaRepositories(basePackages = { "repositories" })
@EntityScan(basePackages = "entities")
OR
选项3(仅用于测试)::将所有类移至主引导应用程序类所在的com.example
。