This is my Controller:
package com.hodor.booking.controller;
import com.hodor.booking.jpa.domain.Vehicle;
import com.hodor.booking.service.VehicleService;
import com.wordnik.swagger.annotations.Api;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/api/v1/vehicles")
@Api(value = "vehicles", description = "Vehicle resource endpoint")
public class VehicleController {
private static final Logger log = LoggerFactory.getLogger(VehicleController.class);
@Autowired
private VehicleService vehicleService;
@RequestMapping(method = RequestMethod.GET)
public List<Vehicle> index() {
log.debug("Getting all vehicles");
return vehicleService.findAll();
}
@RequestMapping(value="/save", method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public Vehicle setVehicle(@RequestBody Vehicle vehicle) {
log.debug("Inserting vehicle");
if (vehicle.getLicensePlate() == null){
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
return vehicleService.saveVehicle(vehicle);
}
}
What I want to achieve in above If-Guard is that, in case the vehicle Object does not have the LicensePlate Member, send back an according HTTP Status Header CONFLICT or something.
I am coming from a Node and Express background and I am used to set my header, send the response and be done with it. However in this case (JPA) it does not seem to work. Any ideas?