Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jax-Rs+;Jersey@Post深度嵌套对象引发内部服务器错误_Java_Json_Rest_Jersey_Jax Rs - Fatal编程技术网

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

Java Jax-Rs+;Jersey@Post深度嵌套对象引发内部服务器错误,java,json,rest,jersey,jax-rs,Java,Json,Rest,Jersey,Jax Rs,我正在尝试使用Postman发布json,并得到内部服务器错误。我认为问题在于解析json时,因为在发布时,我传递的对象也包含嵌套的变量 订单类别: public class Order { private long order_id; private long user_id; private List<OrderDetails> items; private Date order_date; pri

我正在尝试使用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 Product {

    private long prodId;
    private String prodName;
    private String prodDesc;
    private float price;
    .......
}
OrderResource类:我想它甚至没有调用这个方法,因为当我将数据作为字符串传递并将参数更改为字符串时,它正在调用内部并在控制台上显示它。但当我再次将其更改为Order时,它抛出了一个
MessageBodyWriter,它没有为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

因为您使用的是Response、Order等类,所以需要提到@consumes和@products。尝试使用Put方法,尽管Post在这里仍然有效

此外,还要正确配置邮递员、提及内容类型、接受为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!");
    }
..
}
@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!");
    }
..
}