无法通过POST请求返回数据/响应

时间:2020-02-01 23:43:00

标签: java rest spring-boot

以下是我要完成的任务: 1)如果用户评级为否,我想返回422状态代码 2)如果客户评论/标题中包含(Ship,miss,duck,punt,rooster,rooster,mother,bits)之类的字词,则应返回422状态代码 3)否则我应该从模型返回数据。

这是我的控制器类

@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时如何返回数据。

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

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