尝试使用Spring MVC开发后端服务。这里正在编写一个将使用和生成JSON数据的服务。我已经指定了服务的生成和消费格式,但仍然获得HTTP状态415。
Model Class Order.java
package com.limitless.engage.models;
import java.util.List;
/**
* Created by Ram K Bharathi on 9/2/2016.
*/
public class Order {
private String order_id;
private int user_id;
private int tableNo;
private String status;
private String time;
private String date;
private transient List<OrderItems> orderItems;
public Order(){
}
public Order(String order_id,int user_id, int tableNo, String status, String time, String date, List<OrderItems> orderItems) {
this.order_id = order_id;
this.user_id = user_id;
this.tableNo = tableNo;
this.status = status;
this.time = time;
this.date = date;
this.orderItems = orderItems;
}
public String getOrder_id() {
return order_id;
}
public void setOrder_id(String order_id) {
this.order_id = order_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getTableNo() {
return tableNo;
}
public void setTableNo(int tableNo) {
this.tableNo = tableNo;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String data) {
this.date = data;
}
public List<OrderItems> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItems> orderItems) {
this.orderItems = orderItems;
}
@Override
public String toString() {
return "Order{" +
"order_id=" + order_id +
", user_id=" + user_id +
", tableNo=" + tableNo +
", status='" + status + '\'' +
", time='" + time + '\'' +
", date='" + date + '\'' +
", orderItems=" + orderItems +
'}';
}
}
Controller Class OrderController.java
package com.limitless.engage.controllers;
import com.limitless.engage.models.Order;
import com.limitless.engage.responses.OrderResponse;
import com.limitless.engage.services.OrderServices;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* Created by Ram K Bharathi on 9/3/2016.
*/
@RestController
@RequestMapping("/api/order")
public class OrderController {
OrderServices orderServices = new OrderServices();
@RequestMapping(value = "/new", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public ResponseEntity<?> newOrder(@RequestBody Order order)throws Exception{
orderServices.createOrder(order);
OrderResponse orderResponse = new OrderResponse();
orderResponse.setOrderId(order.getOrder_id());
orderResponse.setMessage("success");
return new ResponseEntity<OrderResponse>(orderResponse, HttpStatus.OK);
}
}
我正在使用Chrome Postman扩展程序来检查服务。我也在Postman中设置了内容类型标题。
JSON对象:
{
"order_id":"231",
"user_id":1,
"tableNo":1,
"status":"placed",
"time":"10:00 PM",
"date":"2016-09-04",
"orderItems":[
{
"order_id":"231",
"item_id":1,
"quantity":2,
"taste":"sdbhsfjsdf",
"comment":"shdbfhgeuywe",
"totalPrice":20.00
},
{
"order_id":"231",
"item_id":2,
"quantity":3,
"taste":"sdbhsfjsdf",
"comment":"shdbfhgeuywe",
"totalPrice":45.00
}
]
}