春天的帖子,找不到

时间:2019-01-27 17:14:15

标签: java spring spring-boot

我试图创建我的第一个Spring Back-end简单应用程序,用于存储音乐专辑。不幸的是,我创建的端点都没有工作(我使用邮递员对其进行了测试,请求返回404,未找到)。 以下是我所有与项目相关的文件/代码:

  <button class="btn active" onclick="filterSelection('google')"> Show all</button>

我尝试过的事情:

  1. 使用包添加@ComponentScan:

    @ComponentScan({“ com.musesite.dao”,“ com.musesite.model”,“ com.musesite.musesitebackend”,“ com.musesite.restful”}}

结果:@Repostiory public interface AlbumRepository extends Repository<Album,Long> { @Query(value = "SELECT * FROM album", nativeQuery = true) List<Album> findAlbums(); Album save(Album persisted); } @Service public class AlbumService { @Autowired private AlbumRepository albumRepository; public void saveData(Album album) { this.albumRepository.save(album); } public List<Album> getAllAlbums() { return this.albumRepository.findAlbums(); } @PostConstruct private void init() {} } @Entity @Table(name="album") public class Album { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name="title") private String title; } @RestController public class AlbumController { @Autowired AlbumService albumService; @RequestMapping(method=RequestMethod.POST, path="/album/post") public ResponseEntity<String> addAlbum(@RequestBody Album album) { albumService.saveData(album); return new ResponseEntity<String>(HttpStatus.OK); } @RequestMapping(method=RequestMethod.GET, path="/album/get") Iterable<Album> getAlbums() { return albumService.getAllAlbums(); } } @SpringBootApplication @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class MusesiteBackEndApplication { public static void main(String[] args) { SpringApplication.run(MusesiteBackEndApplication.class, args); } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } }

  1. 添加应创建数据库的dataSource方法。

    @Bean     @ConfigurationProperties(前缀=“ spring.datasource”)     public DataSource dataSource(){         返回DataSourceBuilder.create()。build();     }

结果:无。

  1. Field albumRepository in com.musesite.dao.AlbumService required a bean of type 'com.musesite.dao.AlbumRepository' that could not be found.中添加scanBasePackages

    @SpringBootApplication(scanBasePackages = {         “ com.musesite.dao”,“ com.musesite.model”,“ com.musesite.musesitebackend”,“ com.musesite.restful”}}

结果:SpringBootApplication

不幸的是,他们都没有帮助。

1 个答案:

答案 0 :(得分:-1)

尝试这种方式:

public interface AlbumRepository extends JpaRepository<Album,Long> {

}

您不需要创建本机查询。 您可以使用findAll()和save(album),而无需任何其他代码。 (它们是自动生成的)

---编辑

糟糕!还将以下注释添加到您的配置类之一:

@EnableJpaRepositories