gpt4 book ai didi

json - Spring Boot HttpMediaTypeNotSupportedException

转载 作者:行者123 更新时间:2023-12-01 02:11:30 24 4
gpt4 key购买 nike

我目前正在绞尽脑汁想知道为什么包含参数 @RequestBody Car car 会破坏我的终点。

我对 Spring Boot 非常陌生,并尝试将 json 字符串发布到我的 rest Controller 。

这是我的 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);
}
}

我也使用 jackson 作为我的 json 解析器。我找了几个小时,没有找到任何可以帮助我解释为什么我收到 415 回复的原因。

{
"timestamp": 1425341476013,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Unsupported Media Type",
"path": "/v1/experiences/12"
}

谢谢你的帮助!!

最佳答案

一、 Spring 开机@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>

关于json - Spring Boot HttpMediaTypeNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com