Spring Boot @RequestMapping映射异常 如何禁用根路径/comments访问
问题根因
两个独立的问题叠加导致了你现在遇到的现象:
- 你现在能直接通过
localhost:8080/comments访问到评论接口,根本不是你自定义的CommentController处理的请求。从返回结果的分页元数据、profile链接、标准HAL响应结构就能看出来,这是Spring Data REST根据你注入的CommentRepository自动生成的根路径CRUD端点——你自己写的Controller方法里根本没有实现分页逻辑,不可能返回带page字段的响应结构。 - 你之前想在CommentController类上加
@RequestMapping("/adverts/{id}")当统一路径前缀的写法完全失效:Spring MVC的路径拼接规则是,只要方法上标注的映射路径是以/开头的绝对路径,就会直接用这个路径作为最终映射地址,完全不会拼接类上的前缀。你现在每个方法上都写了带开头斜杠的全路径(比如/adverts/{advertId}/comments),所以类上加不加前缀根本不影响最终映射的路径。
修复步骤
- 关闭Spring Data REST对Comment实体的自动暴露
直接屏蔽掉那个默认生成的/comments访问入口:- 如果不需要Spring Data REST生成任何自动端点,在项目配置文件里加上:
配置生效后,只有显式标注了spring.data.rest.detection-strategy=annotated@RepositoryRestResource的Repository才会被暴露为自动端点,其余Repository都不会自动生成根路径接口。 - 如果需要保留User、Advert等其他实体的自动端点,只屏蔽Comment的,直接给
CommentRepository接口加上注解:@RepositoryRestResource(exported = false) public interface CommentRepository extends PagingAndSortingRepository<Comment, Long> { // 原有方法不变 }
- 如果不需要Spring Data REST生成任何自动端点,在项目配置文件里加上:
- 调整CommentController的路径写法,让类级别的前缀真正生效
把类上的统一路径前缀加上,然后把每个方法上的映射路径改成相对路径(去掉开头的斜杠),Spring MVC会自动拼接类路径和方法路径生成完整地址:@RestController @RequestMapping("/adverts/{advertId}") // 类级别的统一前缀,和方法路径自动拼接 class CommentController { // 原有依赖注入逻辑不变 // 原来的绝对路径 "/adverts/{advertId}/comments" 改成相对路径 "comments" @GetMapping("/comments") CollectionModel<EntityModel<Comment>> all(@PathVariable Long advertId) { // 可顺便修正原逻辑漏洞:按advertId过滤对应广告下的评论,不要直接查全量 List<EntityModel<Comment>> comments = commentRepository.findAllByAdvertId(advertId).stream() .map(comment -> assembler.toModel(comment, advertId)) .collect(Collectors.toList()); return CollectionModel.of(comments, linkTo(methodOn(CommentController.class).all(advertId)).withSelfRel()); } @PostMapping("/comments") Comment newComment(@RequestBody Comment newComment, @PathVariable Long advertId) { newComment.setAdvertId(advertId); return commentRepository.save(newComment); } @GetMapping("/comments/{id}") EntityModel<Comment> one(@PathVariable Long id, @PathVariable Long advertId) { // 原有逻辑不变 Comment comment = commentRepository.findByIdAndAdvertId(id, advertId) .orElseThrow(() -> new CommentNotFoundException(id)); return assembler.toModel(comment, advertId); } // 修正原来的双斜杠笔误,把"/adverts/{advertId}//comments/{id}" 改成单斜杠的相对路径 @PutMapping("/comments/{id}") Comment replaceComment(@RequestBody Comment newComment, @PathVariable Long id, @PathVariable Long advertId) { // 原有逻辑不变 return commentRepository.findByIdAndAdvertId(id, advertId) .map(comment -> { comment.setComment(newComment.getComment()); return commentRepository.save(comment); }) .orElseGet(() -> { newComment.setId(id); newComment.setAdvertId(advertId); return commentRepository.save(newComment); }); } @DeleteMapping("/comments/{id}") void deleteComment(@PathVariable Long id, @PathVariable Long advertId) { // 原有逻辑不变 commentRepository.deleteByIdAndAdvertId(id, advertId); } } - 修正Assembler的链接生成逻辑
现在CommentModelAssembler里生成的链接全部指向根路径/comments,是因为之前调用linkTo方法的时候没有传入对应的advertId参数,导致路径占位符没有被正确填充。改造后重载一个带广告ID参数的toModel方法,生成链接时传入对应广告ID,就能自动生成带正确前缀的链接:@Component public class CommentModelAssembler implements RepresentationModelAssembler<Comment, EntityModel<Comment>> { @Override public EntityModel<Comment> toModel(Comment comment) { // 原单参数方法保留,适配原有调用场景 throw new IllegalStateException("生成评论链接时必须传入对应的advertId"); } public EntityModel<Comment> toModel(Comment comment, Long advertId) { return EntityModel.of(comment, linkTo(methodOn(CommentController.class).one(comment.getId(), advertId)).withSelfRel(), linkTo(methodOn(CommentController.class).all(advertId)).withRel("comments")); } }
验证效果
修改完成重启服务后:
- 直接访问
localhost:8080/comments会返回404,不再暴露未授权的裸接口 - 访问
localhost:8080/adverts/1/comments可以正常获取对应广告下的评论列表,返回的HAL结构链接都会自动带上正确的adverts/{advertId}前缀,不会再出现裸/comments的路径。
内容的提问来源于stack exchange,提问作者Captain Jacky
相关产品推荐
相关产品推荐

