我目前正在绞尽脑汁为什么包含一个参数@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,
“地位”:415,
“错误”:“不支持的媒体类型”,
“exception”:“org.springframework.web.HttpMediaTypeNotSupportedException”,
“message”:“不支持的媒体类型”,
“路径”:“/ v1 / experience / 12”
}
谢谢你的帮助!!
最佳答案 首先,在春季启动时不需要@EnableWebMvc.然后,如果您的REST服务需要生成json或xml使用
@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>