[英]Jax-Rs + Jersey @Post of deeply nested objects throwing internal server error
我正在嘗試使用Postman發布json並收到內部服務器錯誤。 我認為問題出在解析json時,因為在發布時,我傳遞的對象也具有嵌套的變量。
訂單類別:
public class Order {
private long order_id;
private long user_id;
private List<OrderDetails> items;
private Date order_date;
private Double total;
private String status;
......
}
OrderDetails類:
public class OrderDetails {
private Product product;
private Integer quantity;
......
}
產品類別:
public class Product {
private long prodId;
private String prodName;
private String prodDesc;
private float price;
.......
}
OrderResource類:我想它甚至沒有調用此方法,因為當我將數據作為字符串傳遞並將參數更改為string時,它將進入調用內部並將其顯示在控制台上。 但是,當我再次將其更改為Order時,它拋出的MessageBodyWriter not found for media type=application/json, type=class java.util.HashMap, genericType=class java.util.HashMap.
@POST
public Response insertorder(Order order, @Context UriInfo uriInfo) throws ApplicationException {
if (!order.isValid()) {
throw new ApplicationException("Order is invalid!");
}
System.out.println("Order details in Resource: "+order.getUser_id());
Order response = new OrderDAO().InsertOrder(order);
String newId = String.valueOf(response.getOrder_id());
URI url = uriInfo.getAbsolutePathBuilder().path(newId).build();
return Response.created(url)
.status(Status.CREATED)
.entity(response)
.build();
}
OrderDAO類:
public Order InsertOrder(Order order)
{
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
connection = connect();
double total=0;
for(OrderDetails od: order.getItems()) {
total += od.getProduct().getPrice() * od.getQuantity();
}
Order ret = null;
try {
pstmt = connection.prepareStatement("INSERT INTO main.orders_table(\n" +
" user_id, order_date, total,status)\n" +
" VALUES (?, ?, ?)",Statement.RETURN_GENERATED_KEYS);
pstmt.setLong(1, order.getUser_id());
pstmt.setDate(2, (Date) order.getOrder_date());
pstmt.setDouble(3,total);
pstmt.setString(4,"pending");
int affectedRows = pstmt.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}
try (ResultSet generatedKeys = pstmt.getGeneratedKeys()) {
if (generatedKeys.next()) {
System.out.println("Inserted Order Id: "+generatedKeys.getLong(1));
order.setOrder_id(generatedKeys.getLong(1)); //setting the Order ID to the inserted row Id
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
ret = new Order(order.getOrder_id(),order.getUser_id(),order.getOrder_date(),order.getTotal(),order.getStatus());
}
catch (Exception e)
{
System.out.println("Error while inserting Order:" + e);
e.printStackTrace();
} finally {
close(connection, pstmt, rs);
}
return ret;
}
Json String通過郵遞員傳遞:
{
"items": [
{
"product": {
"price": 2,
"prodDesc": "Pakistani Orange",
"prodId": 1002,
"prodName": "ORANGE"
},
"quantity": 5
},
{
"product": {
"price": 3,
"prodDesc": "Kashmir Apple",
"prodId": 1001,
"prodName": "APPLE"
},
"quantity": 5
}
],
"order_date": "2008-07-06T00:00:00+08:00",
"user_id": 2
}
有人可以幫我解決這個問題嗎? TIA
由於您使用的是諸如Response,Order之類的類,因此您需要提及@consumes和@produces。 嘗試使用Put方法,盡管Post仍然可以在這里工作。
還要正確配置郵遞員,分別提及Content-Type,Accept作為application / json。
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response insertorder(Order order, @Context UriInfo uriInfo) throws ApplicationException {
if (!order.isValid()) {
throw new ApplicationException("Order is invalid!");
}
..
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.