我试图使用Postman发布json并获取内部服务器错误。我认为问题在于解析json时,因为在发布I' m时,传递一个包含嵌套变量的对象。
订单类:
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;
}
Json String穿过Postman:
{
"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 :(得分:0)
由于您使用的是Response,Order等类,因此您需要提及@consumes和@produces。尝试使用Put方法,虽然Post仍然可以在这里工作。
还要正确配置邮递员,提及Content-Type,Accept as application / json each。
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="10 empty rows"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="130" y="0" width="200" height="20"/>
<subreportParameter name="evenRow">
<subreportParameterExpression><![CDATA[($V{REPORT_COUNT} % 2 == 0 ? 1 : 0)]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA["Subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>