Jax-Rs + Jersey @Post深度嵌套的对象引发内部服务器错误



我正在尝试使用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;
......
}

订单详情类:

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 类:我想它甚至没有调用此方法,因为当我将数据作为字符串传递并将参数更改为字符串时,它会进入调用内部并在控制台上显示它。但是当我再次将其更改为"订单"时,它抛出了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();
}

订单DAO类:

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 字符串通过邮递员:

{
"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
}

有人可以帮助我解决这个问题吗?蒂亚

由于您使用的是响应、顺序等类,因此您需要提及@consumes和@produces。尝试使用 Put 方法,尽管 Post 仍然可以在这里工作。

还要正确配置邮递员,分别提及内容类型、接受为应用程序/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!");
}
..
}

最新更新