I'm trying to post a json using Postman and getting internal server error. I think the problem is while parsing json because while posting I'm passing an object which has variables which are nested as well.
Order Class:
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 Class:
public class OrderDetails {
private Product product;
private Integer quantity;
......
}
Product Class:
public class Product {
private long prodId;
private String prodName;
private String prodDesc;
private float price;
.......
}
OrderResource class: it is not even calling this method I suppose because when I passed the data as string and changed the parameters to string, it is going inside the call and displaying it on console. But when I change it to Order again, it is throwing an 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 class:
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 passing through 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
}
Can some one help me in solving this. TIA
Since you are using classes like Response, Order, you need to mention @consumes and @produces. Try using Put method, though Post will still work here.
Also configure postman properly, mention Content-Type, Accept as application/json each.
@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!");
}
..
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.