2

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?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

2 Answers2

1

One alternative is to take advantage of Spring's validation support to declarative add validation of your POJOs. Basically, you add annotations to your Vehicle class like:

public class Vehicle {
    @NotNull
    private LicensePlate licensePlate;

    // getters, setters
}

And you add a @Valid annotation to your controller method:

@ResponseBody
public Vehicle setVehicle(@RequestBody @Valid Vehicle vehicle) {
    log.debug("Inserting vehicle");
    return vehicleService.saveVehicle(vehicle);
}

If the validation fails, Spring will return a 400 response.

Make sure that you have JSR-303/JSR-349 Bean Validation implementation such as the Hibernate Validator (it can be used without Hibernate's ORM support) on your classpath.

More information can be found in the validation chapter of the Spring reference docs.

matsev
  • 32,104
  • 16
  • 121
  • 156
  • I am using now the `@NotNull` annotation from this class "javax.validation.constraints.NotNull". Returns a 500 Server Error if null is true, Fair EnoughAs a bonus, is there an annotation like this as well? https://docs.oracle.com/javaee/7/api/javax/validation/constraints/Future.html. Think I found it: `@Future` annotation - http://stackoverflow.com/questions/11624532/where-is-implementation-of-future-defined – Stephan Kristyn May 05 '16 at 19:43
1

What version of Spring MVC are you using? From another post How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?. It states that Spring MVC 4.1 and later using different syntax.

Community
  • 1
  • 1
shokulei
  • 85
  • 7