简体   繁体   English

尝试在springboot中使用post方法时出错

[英]Error while trying to use post metod in springboot

I'm learning Spring Boot rn and I'm getting errors on my way when I'm trying to use POST method.我正在学习 Spring Boot rn,当我尝试使用 POST 方法时出现错误。

HTTP code is 400. HTTP 代码为 400。

That's the error:这就是错误: 在此处输入图像描述

Here is the code of my rest controller c:这是我的 rest controller c 的代码:

package com.example.carsapi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.Optional;

@RestController
@RequestMapping("/cars")
public class CarRestController {

    private final CarService carService;

    @Autowired
    public CarRestController(CarService carService) {
        this.carService = carService;
    }

    @GetMapping
    public ResponseEntity<List> getCars(){
        return new ResponseEntity(carService.findAllCars(), HttpStatus.OK);
    }

    @GetMapping(value = "/id/{id}")
    public ResponseEntity<List> getCarById(@PathVariable Integer id){
        Optional<CarDTO> found = carService.findAllCars().stream().filter(car -> car.getCarId() == id).findFirst();
        if(found.isPresent()){
            return new ResponseEntity(found.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping(value = "/color/{color}")
    public ResponseEntity<List> getCarByColor(@PathVariable String color){
        Optional<CarDTO> found = carService.findAllCars().stream().filter(car -> car.getColor().equals(color)).findFirst();
        if(found.isPresent()){
            return new ResponseEntity(found.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping(value = "/add")
    public ResponseEntity addCar(@RequestBody CarDTO car){
        carService.findAllCars().add(car);
        return new ResponseEntity(HttpStatus.CREATED);
    }
}

And there is my JSON code which I'm using:还有我正在使用的 JSON 代码:

[ 
    {
        "carId": 3,
        "carBrand": "Audi",
        "carModel": "RS6",
        "color": "red"
    }
]

Hope you'll help me I had even tried with @RequestParam , getting all of the values like id , brand , model and color and then adding a new object with these values which I've got from the query but it still didn't work.希望你能帮助我,我什至尝试过使用@RequestParam ,获取所有值,如idbrandmodelcolor ,然后添加一个新的 object ,这些值是我从查询中获得的,但它仍然没有工作。

The request is an array while CarDTO is a single object.请求是一个数组,而CarDTO是单个 object。 You can make the request with a body like this(notice the absence of [] ):您可以使用这样的主体发出请求(注意没有[] ):

{
    "carId": 3,
    "carBrand": "Audi",
    "carModel": "RS6",
    "color": "red"
}

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

相关问题 在springboot中尝试使用AuthenticationManager对用户进行身份验证时出现Stackoverflow错误 - Stackoverflow error while trying to authenticate user using AuthenticationManager in springboot 尝试在现有 gradle 项目上启动 springboot 应用程序时出错 - Getting error while trying to start springboot application on existing gradle project Loopj发布方法失败-尝试发送发布JSON时出错 - Loopj Post method failure - error while trying to send post JSON 我在尝试更新用户时出错,但无法使用 SpringBoot 显示我的用户角色 - I getting an error while i am trying to update an user and not able to show my users roles using SpringBoot 尝试将 OneLogin OIDC 与 SpringBoot 一起使用时出错(错误:redirect_uri_mismatch) - Error trying to use OneLogin OIDC with SpringBoot (error: redirect_uri_mismatch) 尝试启动springboot应用程序时获取服务器时区值“未知”错误 - Getting server time zone value 'unknown' error while trying to start springboot application libgdx - 尝试使用ShapeRenderer时出错 - libgdx - error while trying to use ShapeRenderer SPRINGBOOT中POST请求的I / O错误 - I/O error on POST request for in SPRINGBOOT 上传多部分文件列表时在 springboot 上出错 - getting error on springboot while upload list of multipartfile 运行springboot应用程序时出现错误 - Getting error while running springboot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM