【发布时间】:2020-09-23 05:57:06
【问题描述】:
我的问题是,当我在 CommentController 类上方使用注释 @RequestMapping("/adverts/{id}") 时,我可以同时使用(即)localhost:8080/adverts/1/comments 和 localhost:8080/comments 来访问此类。如何禁用到达localhost:8080/comments?谢谢。
@RestController
class CommentController {
private final CommentRepository commentRepository;
private final AdvertRepository advertRepository;
private final CommentModelAssembler assembler;
CommentController(CommentRepository commentRepository, AdvertRepository advertRepository, CommentModelAssembler assembler) {
this.commentRepository = commentRepository;
this.advertRepository = advertRepository;
this.assembler = assembler;
}
@GetMapping("/adverts/{advertId}/comments")
CollectionModel<EntityModel<Comment>> all() {
List<EntityModel<Comment>> comments =
commentRepository.findAll().stream()
.map(assembler::toModel)
.collect(Collectors.toList());
return CollectionModel.of(comments,
linkTo(methodOn(CommentController.class).all()).withSelfRel());
}
@PostMapping("/adverts/{advertId}/comments")
Comment newComment(@RequestBody Comment newComment) {
return commentRepository.save(newComment);
}
@GetMapping("/adverts/{advertId}/comments/{id}")
EntityModel<Comment> one(@PathVariable Long id) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new CommentNotFoundException(id));
return assembler.toModel(comment);
}
@PutMapping("/adverts/{advertId}//comments/{id}")
Comment replaceComment(@RequestBody Comment newComment,
@PathVariable Long id) {
return commentRepository.findById(id)
.map(comment -> {
comment.setAdvertId(newComment.getAdvertId());
comment.setComment(newComment.getComment());
return commentRepository.save(comment);
})
.orElseGet(() -> {
newComment.setId(id);
return commentRepository.save(newComment);
});
}
@DeleteMapping("/adverts/{advertId}/comments/{id}")
void deleteComment(@PathVariable Long id) {
commentRepository.deleteById(id);
}
}
@Component
public class CommentModelAssembler implements RepresentationModelAssembler<Comment, EntityModel<Comment>> {
@Override
public EntityModel<Comment> toModel(Comment comment) {
return EntityModel.of(comment,
linkTo(methodOn(CommentController.class).one(comment.getId())).withSelfRel(),
linkTo(methodOn(CommentController.class).all()).withRel("comments"));
}
}
@RestController
class UserController {
private final UserRepository repository;
private final UserModelAssembler assembler;
UserController(UserRepository repository, UserModelAssembler assembler) {
this.repository = repository;
this.assembler = assembler;
}
@GetMapping("/users")
CollectionModel<EntityModel<User>> all() {
List<EntityModel<User>> users =
repository.findAll().stream()
.map(assembler::toModel)
.collect(Collectors.toList());
return CollectionModel.of(users,
linkTo(methodOn(UserController.class).all()).withSelfRel());
}
@PostMapping("/users")
User newUser(@RequestBody User newUser) {
return repository.save(newUser);
}
@GetMapping("/users/{id}")
EntityModel<User> one(@PathVariable Long id) {
User user = repository.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
return assembler.toModel(user);
}
@PutMapping("/users/{id}")
User replaceUser(@RequestBody User newUser,
@PathVariable Long id) {
return repository.findById(id)
.map(user -> {
user.setName(newUser.getName());
user.setRole(newUser.getRole());
user.setBoughtVehicles(newUser.getBoughtVehicles());
user.setSoldVehicles(newUser.getSoldVehicles());
return repository.save(user);
})
.orElseGet(() -> {
newUser.setId(id);
return repository.save(newUser);
});
}
@DeleteMapping("/users/{id}")
void deleteUser(@PathVariable Long id) {
repository.deleteById(id);
}
}
@RestController
class AdvertController {
private final AdvertRepository repository;
private final AdvertsModelAssembler assembler;
AdvertController(AdvertRepository repository, AdvertsModelAssembler assembler) {
this.repository = repository;
this.assembler = assembler;
}
@GetMapping("/adverts")
CollectionModel<EntityModel<Advert>> all() {
List<EntityModel<Advert>> adverts =
repository.findAll().stream()
.map(assembler::toModel)
.collect(Collectors.toList());
return CollectionModel.of(adverts,
linkTo(methodOn(AdvertController.class).all()).withSelfRel());
}
@PostMapping("/adverts")
Advert newAdvert(@RequestBody Advert newAdvert) {
return repository.save(newAdvert);
}
@GetMapping("/adverts/{id}")
EntityModel<Advert> one(@PathVariable Long id) {
Advert advert = repository.findById(id)
.orElseThrow(() -> new AdvertNotFoundException(id));
return assembler.toModel(advert);
}
@PutMapping("/adverts/{id}")
Advert replaceAdvert(@RequestBody Advert newAdvert,
@PathVariable Long id) {
return repository.findById(id)
.map(advert -> {
advert.setAutoCategory(newAdvert.getAutoCategory());
advert.setAutoName(newAdvert.getAutoName());
advert.setAutoModel(newAdvert.getAutoModel());
advert.setComments(newAdvert.getComments());
return repository.save(advert);
})
.orElseGet(() -> {
newAdvert.setId(id);
return repository.save(newAdvert);
});
}
@DeleteMapping("/adverts/{id}")
void deleteAdvert(@PathVariable Long id) {
repository.deleteById(id);
}
}
localhost:8080/comments 结果:
{
"_embedded" : {
"comments" : [ {
"advertId" : 1,
"comment" : "That's a nice car!",
"_links" : {
"self" : {
"href" : "http://localhost:8080/comments/3"
},
"comment" : {
"href" : "http://localhost:8080/comments/3"
}
}
}, {
"advertId" : 2,
"comment" : "What a nice color of the car!",
"_links" : {
"self" : {
"href" : "http://localhost:8080/comments/4"
},
"comment" : {
"href" : "http://localhost:8080/comments/4"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/comments"
},
"profile" : {
"href" : "http://localhost:8080/profile/comments"
}
},
"page" : {
"size" : 20,
"totalElements" : 2,
"totalPages" : 1,
"number" : 0
}
}
【问题讨论】:
-
请添加完整的类,因为您的 RequestMapping 中没有汽车。
-
请添加您当前的代码以及到目前为止您尝试过的代码。
-
我添加了 CommentController
-
你能解释一下
reach这个词吗?因为,它们使用不同的 HTTP 方法进行了注释,所以这应该不是问题 -
我想确保只能看到每个用户的 cmets,而不是所有可能的 cmets。所以使用 localhost:8080/cmets 没有意义。
标签: java spring-boot request-mapping