我正在做Spring MVC的演示。我创建了内部带有数据主体的ajax调用发布请求,但是Spring似乎无法识别我的请求及其参数(返回状态415)。它甚至无法调用我的控制器。
var jsonSTr = JSON.stringify(products);
var data = {};
data["products"] = jsonSTr;
$.ajax({
type: "POST",
url: urlReq, --> "/mastercard/render"
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
}).done(function (data, textStatus, jqXHR) {
console.log(textStatus);
});
这是我的 java代码
@RequestMapping(value="/mastercard/render", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ModelAndView renderToMasterCardForm(@RequestBody CheckoutRenderRequest checkoutRenderRequest) {
List<Product> paidProducts = this.jsonToListProduct(checkoutRenderRequest.getProducts());
ModelAndView viewModel = new ModelAndView("mastercardform");
Response response = new Response();
response.status = "ok";
response.url = "mastercardform";
response.price = String.valueOf(paidProducts.parallelStream().collect(Collectors.summarizingDouble(Product::getPrice)).getSum());
;
viewModel.addObject("result", response);
return viewModel;
}
CheckoutRequest 类
private static class CheckoutRenderRequest {
private String products;
public String getProducts() {
return products;
}
public void setProducts(String products) {
this.products = products;
}
}
我曾经尝试过如此糟糕的解决方案,但它仍然无法正常工作。这些站点https://www.javacodegeeks.com/2013/09/spring-mvc-ajax-jquery.html,POST JSON fails with 415 Unsupported media type, Spring 3 mvc,http://www.mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example/引用了我的代码,但它们没有帮助。我真的不知道我在做什么错,请帮忙。
答案 0 :(得分:0)
变量数据不通过JSON.stringify转换,只需使用它即可。
var data = {products: JSON.stringify(xx)};
$.ajax({
data: data,
url: xx
});
答案 1 :(得分:0)
我测试了您的代码,它可以正常工作。
我的ajxa(与您相同)
var jsonSTr = JSON.stringify({"ddd":"sss", "der":"fvgb"});
var data = {};
data["products"] = jsonSTr;
$.ajax({
type: "POST",
url: '/mastercard/render',
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
}).done(function (data, textStatus, jqXHR) {
console.log(textStatus);
});
我的控制器(与您的控制器相同,只是为了测试而改变了返回值)
@RequestMapping(value="/mastercard/render", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ModelAndView renderToMasterCardForm(@RequestBody CheckoutRenderRequest checkoutRenderRequest) {
System.out.println(checkoutRenderRequest.getProducts());
ModelAndView viewModel = new ModelAndView("mastercardform");
viewModel.addObject("result", "Hi");
return viewModel;
}
我的实体(与您相同,但将私有静态类更改为public):
public class CheckoutRenderRequest {
private String products;
public String getProducts() {
return products;
}
public void setProducts(String products) {
this.products = products;
}
}
我的完全控制者:
@Controller
@RequestMapping(value = "/")
public class Test {
@RequestMapping(value = "/")
public String index() {
return "index";
}
@RequestMapping(value="/mastercard/render", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ModelAndView renderToMasterCardForm(@RequestBody CheckoutRenderRequest checkoutRenderRequest) {
System.out.println(checkoutRenderRequest.getProducts());
ModelAndView viewModel = new ModelAndView("mastercardform");
viewModel.addObject("result", "Hi");
return viewModel;
}
}
然后我在index.jsp页面中调用了您的ajax
答案 2 :(得分:0)
增加依赖性
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
尝试检查您的spring mvc配置是否注册了json转换器
<mvc:annotation-driven>
<mvc:message-converters register- defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>