我正在尝试使用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类:我想它甚至没有调用这个方法,因为当我将数据作为字符串传递并将参数更改为字符串时,它会进入调用内部并在控制台上显示它。但当我再次将其更改为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;
}
通过Postman传递的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
}
有人能帮我解决这个问题吗?提亚
发布于 2017-07-03 19:04:47
由于您使用的是Response、Order等类,因此需要提到@consumes和@produces。尝试使用Put方法,尽管Post在这里仍然有效。
另外,正确配置postman,提到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!");
}
..
}
https://stackoverflow.com/questions/44888443
复制相似问题