繁体   English   中英

Spring 启动 POST 收到错误请求

[英]Spring Boot POST getting bad request

我有 400 错误请求错误,它只发生在 post 请求中,我正在使用 @PostMapping。 当我使用 postman 发送 JSON object 时它不起作用,我收到 400 错误请求错误。 如果有人可以请帮助。

这是 Controller:

 package com.backend.ShoppingList.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.backend.ShoppingList.model.Store;
import com.backend.ShoppingList.service.StoreService;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/")
public class Control {

    @Autowired
    StoreService storeService;


    // create stores
    @PostMapping(path = "/createstore", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Store> create(@RequestBody Store newStore) throws Exception {
        Store store = storeService.saveStore(newStore);
        if (store == null) {
            throw new Exception();
        } else {
            return new ResponseEntity<>(store, HttpStatus.CREATED);
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM