Reputation: 15752
This is my Controller:
package com.hodor.booking.controller;
import com.audi.interview.booking.jpa.domain.Vehicle;
import com.audi.interview.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
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")
public void addVehicle(@RequestBody Car car, Vehicle vehicle) {
log.debug("Inserting vehicle");
Vehicle testVehicle1 = new Vehicle();
testVehicle1.setLicensePlate("M-1234");
testVehicle1.setModel("M5");
testVehicle1.setColor("Grey");
testVehicle1.setActive(true);
testVehicle1.setVin("8765-4321");
testVehicle1.setValidTill(DateUtils.addYears(new Date(), 1));
vehicleService.saveVehicle(testVehicle1);
}
}
}
Now "@RequestBody" and "Car" cannot be resolved. I am new to Java and Spring and I followed documentation here: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates
How can I consume JSON data sent in my POST request?
Upvotes: 1
Views: 1002
Reputation: 2456
You are having controller method signature like this:
public void addVehicle(@RequestBody Car car, Vehicle vehicle) {
}
I am assuming that - (Vehicle vehicle) is having little or no significance as you can not pass two dtos to your controller method - Ref - Spring MVC controller with multiple @RequestBody. If thats the case, then you can probably remove that from your method signature.
Then you can create Car as a JSON structure and pass it on to your controller using any REST client which should work for you. Ref - Using Postman to test REST persistence endpoints
Upvotes: 1