Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting status 500 error while postmapping in springboot

I got a status 500 error while I was trying to post some data by HTTP method

That’s my service from which I’m getting a list of cars (CarDTO)
I’ve tried creating other methods for adding but it doesn’t.

Also, I can see a log that says that the error is in line where I’m adding an object to a list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error

@Service
public class CarService {

    private List<CarDTO> carList = Arrays.asList(
            new CarDTO(1, "Audi", "A6", "black"),
            new CarDTO(2, "BMW", "E36", "pink")
    );

    public List<CarDTO> findAllCars() {
        return carList;
    }
}

And that’s my rest controller:

@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);
    }
}

How to fix this issue?

>Solution :

The issue is that Arrays.asList() creates a fixed-size list and thus you can’t add anything to it (reference documentation at Arrays.asList).

Use the following instead:

private List<CarDTO> carList = new ArrayList() {{
    add(new CarDTO(1, "Audi", "A6", "black"));
    add(new CarDTO(2, "BMW", "E36", "pink"));
}};

You can find other possibilities at How to init list one line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading