我目前正在绞尽脑汁为什么包含一个参数@RequestBody Car car打破了我的终点。
我是Spring新手并且尝试将json字符串发布到我的休息控制器。
这是我的控制器:
@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);
}
}
我也使用jackson作为我的json解析器。我找了几个小时,发现没有任何东西可以帮助我解释为什么我得到415回复。
{
"timestamp": 1425341476013,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Unsupported Media Type",
"path": "/v1/experiences/12"
}
感谢您的帮助!!
答案 0 :(得分:2)
首先,在春季启动时@EnableWebMvc
不需要。然后,如果您的REST服务需要使用
@RequestMapping(value = "properties", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.POST)
测试您的服务
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();
按application/json
或application/xml
替换标头。如果您使用xml,请添加此依赖项
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
答案 1 :(得分:0)
如果您使用的是JSON,请移除consumes={...}
的{{1}}部分,确保您实际上正在发布JSON并将@RequestMapping
设置为Content-type
。< / p>