org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
Reason for exception: The parameter format required by the back-end interface API is json, but the data format submitted by our front-end is form.
This exception was encountered when I was doing Alipay payment. There are two ways to solve it, as follows:
Modification method 1: Modify the parameter receiving format of the interface API to receive the form form.
Remove the red annotation @RequestBody , because this annotation indicates that the parameter format received by j is json
@Api(description = "支付宝相关接口")
@RestController
@RequestMapping("/alipay")
public class AlipayController {
@Resource
private AlipayService alipayService;
@ApiOperation(value = "支付宝支付")
@PostMapping(value = "pay", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void pay(
@Validated @RequestBody TripOrderPay order,
HttpServletRequest request, HttpServletResponse response)
throws AlipayApiException, IOException {
String result = alipayService.alipayTradePagePay(order.getOut_trade_no(), order.getTotal_amount(), order.getSubject(), order.getBody());
response.setContentType("text/html; charset=utf-8");
response.getWriter().print(result);
}
}
Modification method 2: Convert the passed parameter format to json format, and set the http request header to content-type: application/json;charset=UTF-8
$.ajax({
type: "POST",
contentType: "application/json;charset=UTF-8",
url: "/alipay/pay",
data: JSON.stringify(data.field),
dataType: 'json',
success: function(result) {
if(result.code == 0) {
layer.msg('Payment successful!');
} else {
layer.msg(result.msg);
}
}
});