2017-07-03 23 views
0

我試圖發佈一個使用Postman的Json並獲取內部服務器錯誤。我認爲問題是在解析json的時候,因爲在發佈時我傳遞了一個嵌套了變量的對象。Jax-Rs + Jersey @深度嵌套對象拋出內部服務器錯誤

Order類:

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(); 
    } 

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字符串經過郵差:

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

回答

0

由於您使用的是響應,訂單等類,因此您需要提及@consumes和@produces。嘗試使用Put方法,但Post仍然可以在這裏使用。

還要正確配置郵遞員,提到Content-Type,接受爲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!"); 
    } 
.. 
} 
相關問題