gpt4 book ai didi

java - 无法从 POST 请求返回数据/响应

转载 作者:行者123 更新时间:2023-12-02 09:03:08 24 4
gpt4 key购买 nike

以下是我想要实现的任务:1)如果用户评分是否定的,我想返回422状态码2) 如果客户评论/标题包含 (Ship,miss,duck,punt,rooster,mother,bits) 等字样,应返回 422 状态代码3)否则我应该从模型返回数据。

这是我的 Controller 类

@RestController
public class CustomerReviewController {
@Autowired
private ProductDao productDao;

@Autowired
private UserDao userDao;

@Autowired
private CustomerReviewService customerReviewService;

@GetMapping({ "products/{productId:\\d+}/reviews" })
public List<CustomerReviewModel> getReviews(@PathVariable final Long productId,
@RequestParam(required = false) final Double ratingFrom,
@RequestParam(required = false) final Double ratingTo) {
final ProductModel product = productDao.findOne(productId);
if (product == null) {
throw new ProductNotFoundException(productId);
}
List<CustomerReviewModel> abc = customerReviewService.getReviewsForProduct(product);
List<CustomerReviewModel> abcd = new ArrayList<CustomerReviewModel>();

Double start = 0.0d;
Double end = 0.0d;

for (int i = 0; i < abc.size(); i++) {
CustomerReviewModel temp = abc.get(i);
if (ratingFrom == null && ratingTo == null) {
return abc;
} else if (ratingFrom == null) {
end = ratingTo;
if (temp.getRating() <= end) {
abcd.add(temp);
}

}

else if (ratingTo == null) {
start = ratingFrom;
if (temp.getRating() >= start) {
abcd.add(temp);
}
} else {

if (ratingFrom < ratingTo) {
start = ratingFrom;
end = ratingTo;
} else {
start = ratingTo;
end = ratingFrom;
}
if ((temp.getRating() >= start && temp.getRating() <= end)) {
abcd.add(temp);
}
}

}
return abcd;

}

ResponseEntity abc;

@PostMapping({ "products/{productId:\\d+}/users/{userId:\\d+}/reviews" })
public CustomerReviewModel createReview(@PathVariable final Long userId, @PathVariable final Long productId,
@RequestBody final CustomerReviewForm customerReviewForm) {
final ProductModel product = productDao.findOne(productId);
if (product == null) {
throw new ProductNotFoundException(productId);
}

final UserModel user = userDao.findOne(userId);
if (user == null) {
throw new UserNotFoundException(userId);
}

// return new ResponseEntity(HttpStatus.UNPROCESSABLE_ENTITY);

Set<String> al1 = new HashSet<String>();
al1.add("Ship");
al1.add("Miss");
al1.add("Duck");
al1.add("Punt");
al1.add("Rooster");
al1.add("Mother");
al1.add("Bits");

for (String al2 : al1) {
if ((customerReviewForm.getComment().toLowerCase().contains(al2.toLowerCase()))
|| (customerReviewForm.getRating() < 0) ||(customerReviewForm.getHeadline().toLowerCase().contains(al2.toLowerCase()))) {
// abc = getResponse();
abc = new ResponseEntity(HttpStatus.UNPROCESSABLE_ENTITY);
//return abc;
}

}

//return abc;
// return
return customerReviewService.createCustomerReview(customerReviewForm.getRating(),
customerReviewForm.getHeadline(), customerReviewForm.getComment(), product,
user);
}

@PostMapping({ "products" })
public ProductModel createProduct() {
final ProductModel product = new ProductModel();
productDao.save(product);
return product;
}

@PostMapping({ "users" })
public UserModel createUser() {
final UserModel user = new UserModel();
userDao.save(user);
return user;
}

@DeleteMapping({ "reviews/{reviewId:\\d+}" })
public void deleteReview(@PathVariable final Long reviewId) {
customerReviewService.deleteCustomerReview(reviewId);
}
}

CustomerReviewFormClass:

public class CustomerReviewForm implements Serializable
{
private Double rating;
private String headline;
private String comment;

客户评论模型类:

public class CustomerReviewModel implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String headline;
private String comment;
private Double rating;

如果我将POST方法的返回类型更改为ResponseEntity,当响应为200时如何返回数据。

最佳答案

你可以这样做:

return new ResponseEntity<T>(data, HttpStatus.OK);

关于java - 无法从 POST 请求返回数据/响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com