我已经寻找了这个问题的各种可能性,但不知道为什么会发生。它想用javascript在localstorage中保存一个对象,然后获取这个对象并将数据发送到我的spring启动应用程序。但是当我调用ajax时,它会给我一个错误,如下所示
java.lang.IllegalArgumentException: Invalid character found in the request target [/product/cart/items?{%22cartItems%22:[{%22itemId%22:%22asfasfa%22,%22itemCount%22:2},{%22itemId%22:%22ijhar%22,%22itemCount%22:3}]}]. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:491) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.36.jar:9.0.36]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_265]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_265]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.36.jar:9.0.36]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
javascript代码
$(document).ready(function() {
function getAllItemsFromCart() {
let product;
product = JSON.parse(localStorage.getItem("items"));
return product;
}
let itemsList = {};
let cartItems = [];
let items = {};
items['itemId'] = 'asfasfa';
items['itemCount'] = 2;
cartItems.push(items);
items = {};
items['itemId'] = 'ijhar';
items['itemCount'] = 3;
cartItems.push(items);
itemsList['cartItems'] = cartItems;
localStorage.setItem('items', JSON.stringify(itemsList));
let itemsList1 = getAllItemsFromCart();
$.ajax({
url: "[[@{/product/cart/items}]]",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "GET",
data: JSON.stringify(itemsList1),
dataType: "json",
success: function(response, status, xhr) {
console.log("This is success message")
},
error: function(response) {
console.debug(response.status + ":" + response.statusText);
},
beforeSend: function() {
//talman.showAjaxLoader();
},
complete: function() {
//talman.closeAjaxLoader();
}
});
});
java控制器
@GetMapping(value = "/cart/items",consumes = "application/json", produces = "application/json")
@ResponseBody
public String addProductToCart(@RequestBody CartItemsBean itemList){
return "sucess";
}
豆
class ItemsBean {
private String itemId;
private String itemCount;
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getItemCount() {
return itemCount;
}
public void setItemCount(String itemCount) {
this.itemCount = itemCount;
}
}
public class CartItemsBean {
private List<ItemsBean> cartItems;
public List<ItemsBean> getCartItems() {
return cartItems;
}
public void setCartItems(List<ItemsBean> cartItems) {
this.cartItems = cartItems;
}
}
1条答案
按热度按时间g2ieeal71#
出于安全原因,SpringBoot不接受queryparams上的某些特殊字符。
但我认为在您的情况下,您需要使用json.stringfy来 Package 它。
顺便说一句,它是有点奇怪,得到接收机构或作为一个职位或补丁。