Reputation: 443
I am currently racking my head as to why including a parameter @RequestBody Car car breaks my end point.
I am very new to Spring boot and and trying to post a json string to my rest controller.
Here's my controller:
@RestController
@RequestMapping("/v1/car")
@EnableWebMvc
public class CarController {
private static final Log LOGGER = LogFactory.getLog(CarController.class);
@Autowired
private CarService carService;
@RequestMapping(value="/{accountId}", method = RequestMethod.POST, consumes={"text/plain", "application/*"})
ResponseEntity<?> start(@PathVariable final Integer accountId, @RequestBody Car car) {
System.out.println("E: "+accountId);
final long tid = Thread.currentThread().getId();
final Boolean status = this.smarterWorkFlowService.startWorkFlow(accountId, car);
return new ResponseEntity<Car>(new Car(), HttpStatus.ACCEPTED);
}
}
I am using jackson as my json parser too. I looked for hours and have found nothing that can help me explain why I am getting a response of 415 back.
{
"timestamp": 1425341476013,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Unsupported Media Type",
"path": "/v1/experiences/12"
}
Thanks for any help!!
Upvotes: 4
Views: 14831
Reputation: 6549
First, in spring boot @EnableWebMvc
is not needed. Then, if your REST service need to produce json or xml use
@RequestMapping(value = "properties", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.POST)
Test your service
HttpHeaders headers = new HttpHeaders();
headers.add("Content-type", header);
headers.add("Accept", header);
UIProperty uiProperty = new UIProperty();
uiProperty.setPassword("emelendez");
uiProperty.setUser("emelendez");
HttpEntity entity = new HttpEntity(uiProperty, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange("http://localhost:8080/properties/1", HttpMethod.POST, entity,String.class);
return response.getBody();
Replace header by application/json
or application/xml
. If you are usin xml, add this dependency
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Upvotes: 3
Reputation: 8774
If you're using JSON, remove the consumes={...}
part of your @RequestMapping
, make sure you're actually POSTing JSON and set the Content-type
to application/json
.
Upvotes: 1