2016-05-13 32 views
0

我有以下看法上側JSON請求Spring mvc服務器。嘗試後JSON到控制器(服務器拒絕這個請求)

的index.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript" > 
$(document).ready(function(){ 
    sendAjax(); 
}); 

function sendAjax() { 

$.ajax({ 
    url: "/shop/main/order/model", 
    type: 'POST', 
    dataType: 'json', 
    data: " { \"totalPrice\": 10, \"custumerName\":\"vasiya\", \"ItemsList\": [ { \"id\": 1,\"name\":\"qqq\", \"price\": 10, \"count\": 2 } ] }", 
    contentType: 'application/json', 
    mimeType: 'application/json', 
    success: function(data) { 
     alert(data.totalPrice + " " + data.custumerName); 
    }, 
    error:function(data,status,er) { 
     alert("error: "+data+" status: "+status+" er:"+er); 
    } 
}); 
} 
</script> 

</body> 
</html> 

controller看起來像這樣

@Controller 
@RequestMapping("/order") 
public class OrderController { 

    @RequestMapping(value="/model", method = RequestMethod.POST) 
     public @ResponseBody OrderModel post(@RequestBody final OrderModel model) { 
      System.out.println(model.getItemsList()); 
      System.out.println(model.getTotalPrice()); 

      return model; 
     } 


    } 
} 

OrderModel.java

public class OrderModel implements Serializable { 
    private static final long serialVersionUID = 1L; 
    String custumerName; 
    int totalPrice; 
    Items[] itemsList; 

    public void setTotalPrice(int totalPrice) { 
     this.totalPrice = totalPrice; 
    } 

    public void setItemsList(Items[] itemsList) { 
     this.itemsList = itemsList; 
    } 

    public int getTotalPrice() { 

     return totalPrice; 
    } 

    public Items[] getItemsList() { 
     return itemsList; 
    } 

    public void setCustumerName(String custumerName) { 
     this.custumerName = custumerName; 
    } 

    public String getCustumerName() { 

     return custumerName; 
    } 

    public OrderModel(String custumerName, int totalPrice, Items[] itemsList) { 
     this.custumerName = custumerName; 

     this.totalPrice = totalPrice; 
     this.itemsList = itemsList; 
    } 
} 

Items.java是一個簡單的POJO類,它是爲映射hibernate實體而設計的,它只包含String和long類型字段。

所以當我試圖訪問index.html它不會發回任何迴應。在瀏覽器的控制檯我得到如下:

POST http://localhost:8080/shop/main/order/model 415()

HTTP狀態415 -

類型狀態報告

消息

介紹:服務器拒絕此請求,因爲請求實體 的格式不受請求的資源爲 請求的方法。

+0

乍一看,我覺得有一點奇怪的\」請求體。試試這個數據:{totalPrice:10,custumerName:'vasiya',ItemsList:[{id:1,name:'qqq',price:10,count:2}]},' – MaVVamaldo

+0

nope,問題仍然存在 – ketchyn

+0

讓我指向你[這裏](http://stackoverflow.com/a/36084619/740480)。也許這是'@ RequestMapping'中缺少'consumes ='application/json'' – MaVVamaldo

回答

0

2我從你的代碼中發現的事情。 1.您需要在應用程序中使用messageConverter。

要添加消息轉換器,您需要在您的pom中添加這2個依賴項。檢查適用於您的正確版本。

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.7.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 

2.在您的dispature-servlet.xml或您正在使用的任何mvc配置文件中添加以下bean。

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
     </list> 
    </property> 
</bean> 

在此之後,您將不會得到415,因爲您的應用程序將找到正確的消息轉換器。

  1. 您的OrderModel實體缺少默認的構造函數,一旦您完成2個以上的步驟,將會出錯。所以添加一個默認的構造函數。

    public OrderModel() { } 
    
+0

你該死的權利!非常感謝。但沒有額外的bean定義,它工作正常。它真的有必要嗎?我已經在POM中使用了jackson-mapper-asl,但是沒有使用jackson-databind和默認構造函數.-看起來這兩個是主要問題。 – ketchyn

+0

你可以給我發送你的dispatcher-servlet.xml嗎?正如我嘗試沒有消息轉換器,並得到415.所以我加了它。 –

+0

我想我在我的上下文配置文件中缺少。添加完後。我們不需要定義那個消息轉換器bean。 –

0

您發送ItemsList但在你的類是itemsList

+0

哦謝謝什麼尷尬的錯誤,但它仍然是相同的結果 – ketchyn