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

标签 java rest spring-boot

以下是我想要实现的任务: 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/

相关文章:

java - 未能关闭 Spring Boot 安全配置

spring-boot - 哪个 CoroutineScope 用于 Spring Boot WebFlux 端点

java - 如何使用maven构建spring boot应用程序的库jar

java - 使用 Spring 连接不在列表中实现标记接口(interface)的 bean

java - 在spark上找不到reduceByKey方法

java - 如何使用@BeforeTest注释获取testng中的方法名称

regex - @EnableFeignClients 包扫描 - spring boot

java - 如何阻止 jackson 在反序列化时更改日期时间

java - 使用 XMLSchema : Best Practices? (Java) 配置解析代码中的 XML 验证

rest - 如何使用 json 补丁更新集合