大家好(首先发布在这里!)
我遇到了一个我正在开发的简单Web应用程序的问题。
我使用spring boot创建了一个休息服务(POST),但是当我从前端尝试这个服务时,我无法在@RequestBody上检索任何值。
调用控制器的javascript是以下
bookStore.html
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django_celery_beat/schedulers.py", line 238, in sync
with transaction.atomic():
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/transaction.py", line 147, in __enter__
if not connection.get_autocommit():
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 378, in get_autocommit
self.ensure_connection()
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
OrderController.java
//Submit shopping cart to the backend to be saved
checkout : function() {
//Place holder for the POST Request
this.$http.post('/createOrder', {
'shopCart' : {'basket' : this.shoppingCart}
});
//END Post request
}
ShoppingCart.java
@RequestMapping(value = "/createOrder",
method = RequestMethod.POST)
public String createOrder(@RequestBody ShoppingCart shopCart, Model model) {
Order order = new Order();
Float totalPrice = 0f;
order.setCreationDate(new Date());
List<String> bookIdList = new ArrayList<>();
for (Book book : shopCart.getBasket()) {
bookIdList.add(book.getId());
totalPrice = totalPrice + book.getPrice();
}
order.setItemList(bookIdList);
order.setTotalOrder(totalPrice);
orderRepo.save(order);
List<Order> orderList = orderRepo.findAll();
model.addAttribute("addedOrder", order);
model.addAttribute("orderList", orderList);
return "orderList";
}
}
请注意,shoppingCart被定义为VUE js中的对象数组。当我设置此值时,它看起来像这样
public class ShoppingCart {
private List<Book> basket;
public List<Book> getBasket() {
return basket;
}
public void setBasket(List<Book> basket) {
this.basket = basket;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BookList [shoppingCart=");
builder.append(basket);
builder.append("]");
return builder.toString();
}
问题是shopCart始终为null。我错过了什么吗?它似乎是一个基本的事情,它与我之前完成的RequestParam控制器一起工作
答案 0 :(得分:0)
这是一篇旧帖子,但此信息可能对某人有所帮助。在顶部使用 @RequestBody 完成并映射到模型的方式应该与 @RestController 一起使用,但有一个小例外,即响应不应是字符串,而可能是具有更新值的模型。
就我而言,我遇到了一个问题,即从 SwaggerUI 错误地导入了 @RequestBody 而不是“ import org.springframework.web.bind.annotation.RequestBody;" 这给了我一个难以捕捉的错误。
只要 pojo 是可序列化的,它就应该可以工作。此外,如果您从手动构建的字符串发送 JSON,请确保在属性的大小写中遵循命名约定。