【发布时间】:2021-07-21 16:42:15
【问题描述】:
作为我大学项目的一部分,我被要求实现一个简单的 spring-boot 应用程序(仅限后端),它可以通过 HTTP 请求与 Postman 通信。
该项目构建在控制器-服务-存储库架构中,仅包含 1 个实体(具有字符串内容的帖子对象)和 2 个端点(创建新帖子,获取所有帖子)。
我知道在 spring-boot 中配置 bean 有几种方法:
- 使用外部 XML 文件。
- 带有@Configuration注解和@Bean注解
- 带@Component注解(@RestController,@Service,@JpaRepository)
第三种方式效果很好,但我被要求实施第二种方式,我真的很难让它发挥作用。
我得到:
ServletException: 循环视图路径 [post]: 将再次分派回当前处理程序 URL [/post]。检查您的 ViewResolver 设置!
试图探索这个异常,我确实设法通过添加这个 maven 依赖来“解决”它:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.2</version>
</dependency>
导致:
"org.thymeleaf.exceptions.TemplateInputException: Error resolving template [post], template might not exist or might not be accessible by any of the configured Template Resolvers"
我做错了什么?
配置类:
@Configuration
@EnableJpaRepositories(basePackages = {
"com.example.microblog.post.domain.repository"
})
public class ApplicationBeans {
@Bean
public PostController postController(PostService postService){
return new PostController(postService);
}
@Bean
public PostService postService(){
return new PostService();
}
}
控制器类:
@AllArgsConstructor
@RequestMapping(path = "post")
public class PostController {
@Autowired
private PostService service;
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("")
public PostEntity create(@RequestBody PostDto dto) {
return service.create(dto);
}
@GetMapping("/all")
@CrossOrigin(origins = "http://localhost:4200")
public List<PostEntity> getAll() {
return service.getAll();
}
}
服务类:
@Transactional
public class PostService {
@Autowired
private PostRepository PostRepository;
public PostEntity create(PostDto dto){
PostEntity newPost = new PostEntity(dto.getContent());
return PostRepository.save(newPost);
}
public List<PostEntity> getAll(){
return PostRepository.findAll();
}
存储库类:
public interface PostRepository extends JpaRepository<PostEntity,Long> {}
【问题讨论】:
-
请发布您的实际代码,而不是您的代码图像。更容易全面了解情况,而且帮助尝试帮助的人花费的时间也更少。
-
请注意,还有另一个选项,这是我用于大部分 bean 的选项:您可以在
@Configuration上使用@Import(MyService.class)(包括您的@SpringBootApplication,用于小型程序)。
标签: java spring spring-boot