[Java Spring MVC] @GetMapping, @PostMapping, @PutMapping, @PatchMapping & @DeleteMapping
package com.example.ec.web; import com.example.ec.domain.Tour; import com.example.ec.domain.TourRating; import com.example.ec.domain.TourRatingPk; import com.example.ec.repo.TourRatingRepository; import com.example.ec.repo.TourRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.stream.Collectors; @RestController @RequestMapping(path = "/tours/{tourId}/ratings") public class TourRatingController { TourRatingRepository tourRatingRepository; TourRepository tourRepository; @Autowired public TourRatingController(TourRatingRepository tourRatingRepository, TourRepository tourRepository) { this.tourRatingRepository = tourRatingRepository; this.tourRepository = tourRepository; } protected TourRatingController() {} @PostMapping @ResponseStatus(HttpStatus.CREATED) public void createTourRating(@PathVariable(value = "tourId") int tourId, @RequestBody @Validated RatingDto ratingDto) { Tour tour = verifyTour(tourId); tourRatingRepository.save(new TourRating( new TourRatingPk(tour, ratingDto.getCustomerId()), ratingDto.getScore(), ratingDto.getComment())); } @GetMapping public List<RatingDto> getAllRatingsForTour(@PathVariable(value = "tourId") int tourId) { verifyTour(tourId); return tourRatingRepository.findByPkTourId(tourId) // stream it .stream() // map to dto .map(RatingDto::new) // convert to a list .collect(Collectors.toList()); } @GetMapping(path = "/average") public Map<String, Double> getAverage(@PathVariable(value = "tourId") int tourId) { verifyTour(tourId); return Map.of("average", tourRatingRepository.findByPkTourId(tourId).stream() .mapToInt(TourRating::getScore).average() .orElseThrow(() -> new NoSuchElementException("Tour has no Ratings"))); } /** * Update score and comment of a Tour Rating * * @param tourId tour identifier * @param ratingDto rating Data Transfer Object * @return The modified Rating DTO. */ @PutMapping public RatingDto updateWithPut(@PathVariable(value = "tourId") int tourId, @RequestBody @Validated RatingDto ratingDto) { TourRating rating = verifyTourRating(tourId, ratingDto.getCustomerId()); rating.setScore(ratingDto.getScore()); rating.setComment(ratingDto.getComment()); return new RatingDto(tourRatingRepository.save(rating)); } /** * Update score or comment of a Tour Rating * * @param tourId tour identifier * @param ratingDto rating Data Transfer Object * @return The modified Rating DTO. */ @PatchMapping public RatingDto updateWithPatch(@PathVariable(value = "tourId") int tourId, @RequestBody @Validated RatingDto ratingDto) { TourRating rating = verifyTourRating(tourId, ratingDto.getCustomerId()); if (ratingDto.getScore() != null) { rating.setScore(ratingDto.getScore()); } if (ratingDto.getComment() != null) { rating.setComment(ratingDto.getComment()); } return new RatingDto(tourRatingRepository.save(rating)); } /** * Delete a Rating of a tour made by a customer * * @param tourId tour identifier * @param customerId customer identifier */ @DeleteMapping(path = "/{customerId}") public void delete(@PathVariable(value = "tourId") int tourId, @PathVariable(value = "customerId") int customerId) { TourRating rating = verifyTourRating(tourId, customerId); tourRatingRepository.delete(rating); } /** * Verify and return the TourRating for a particular tourId and Customer * @param tourId tour identifier * @param customerId customer identifier * @return the found TourRating * @throws NoSuchElementException if no TourRating found */ private TourRating verifyTourRating(int tourId, int customerId) throws NoSuchElementException { return tourRatingRepository.findByPkTourIdAndPkCustomerId(tourId, customerId).orElseThrow(() -> new NoSuchElementException("Tour-Rating pair for request(" + tourId + " for customer" + customerId)); } /** * Verify and return the Tour given a tourId. * * @param tourId tour identifier * @return the found Tour * @throws NoSuchElementException if no Tour found. */ private Tour verifyTour(int tourId) throws NoSuchElementException { return tourRepository.findById(tourId).orElseThrow(() -> new NoSuchElementException("Tour does not exist " + tourId)); } /** * Exception handler if NoSuchElementException is thrown in this Controller * * @param ex exception * @return Error message String. */ @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(NoSuchElementException.class) public String return400(NoSuchElementException ex) { return ex.getMessage(); } }
DTO:
package com.example.ec.web; import com.example.ec.domain.TourRating; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * Data Transfer Object for Rating a Tour * */ public class RatingDto { @Min(0) @Max(5) private Integer score; @Size(max = 255) private String comment; @NotNull private Integer customerId; /** * Construct a RatingDto from a fully instantiated TourRating. * * @param tourRating Tour Rating Object */ public RatingDto(TourRating tourRating) { this(tourRating.getScore(), tourRating.getComment(), tourRating.getPk().getCustomerId()); } /** * Constructor to fully initialize the RatingDto * * @param score score 1-5 * @param comment comment * @param customerId customer identifier */ private RatingDto(Integer score, String comment, Integer customerId) { this.score = score; this.comment = comment; this.customerId = customerId; } protected RatingDto() {} public Integer getScore() { return score; } public void setScore(Integer score) { this.score = score; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public Integer getCustomerId() { return customerId; } public void setCustomerId(Integer customerId) { this.customerId = customerId; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-12-16 [Algorithm] 118. Pascal's Triangle
2019-12-16 [React] Create a Custom Suspending Image Component
2016-12-16 [RxJS] Flatten a higher order observable with concatAll in RxJS
2016-12-16 [RxJS] Flatten a higher order observable with mergeAll in RxJS
2016-12-16 [Compose] 8. A curated collection of Monoids and their uses
2016-12-16 [Javascript] Combine Objects with Object.assign and Lodash merge
2014-12-16 [AngularJS] Introduction to ui-router