2013-02-28 59 views
6

我不明白爲什麼我不能將更新發布到我的控制器。我正在嘗試通過Chrome插件提交json數據。最終,我將對請求使用角度。我檢查了其他的stackoverflow文章,似乎我有他們建議的一切。Spring MVC應用程序不接受JSON

爲什麼它的價值我有一個GET請求到工作沒有問題的同一個控制器。

HTTP Status 415 - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 

我的服務器日誌顯示以下

INFO - Mapped "{[/service/products/addProduct],methods=[POST],params=[],headers=[],consumes=[application/json],produces=[],custom=[]}" onto public void com.cr.controllers.ProductsController.addProduct(com.cr.entity.Products) 

郵政解決

http://localhost:8082/service/products/addProduct 

數據被張貼

{ 
    "productId": 2, 
    "productModel": "Product Model 2", 
    "productName": "Product Name 2", 
    "dateAdded": 1361880001000, 
    "productWeight": 2, 
    "productStatus": "Hidden", 
    "productTaxClass": { 
     "taxId": 2, 
     "taxClassTitle": "High Tax Class", 
     "taxClassDescription": "This is a high tax class", 
     "lastModified": 1361880001000, 
     "dateAdded": 1361880001000 
    }, 
    "productImages": { 
     "imageId": 2, 
     "imageDescription": "Product Image 2", 
     "imageTitle": "Image 2", 
     "imagePath": "prd_02.jpg", 
     "imageRelation": 1 
    }, 
    "productManufacturer": { 
     "manufacturerId": 2, 
     "manufacturerName": "Factory 2", 
     "manufacturerImage": null 
    }, 
    "quantityAvailable": 4, 
    "quantityInWarehouse": 4, 
    "stockAlert": 1, 
    "productCost": 1, 
    "productRetail": 1, 
    "productPrice": 1, 
    "productSalePrice": 1, 
    "saleInd": null, 
    "productSku": null, 
    "backOrderMessage": null, 
    "inStockMessage": null, 
    "outOfStockMessage": null, 
    "manufacturerproductsku": null, 
    "productDescriptionId": { 
     "productTextId": 2, 
     "productTextData": "Este es en espanol", 
     "lastModified": 1361793601000 
    } 
} 

控制器映射

@RequestMapping(value = "/service/products/addProduct", 
     consumes = "application/json", 
     method= RequestMethod.POST) 
public @ResponseBody void addProduct(@RequestBody Products products){ 
    productsDao.createProduct(products); 
} 

的web.xml

<servlet-mapping> 
     <servlet-name>cr</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <filter> 
     <filter-name>httpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>httpMethodFilter</filter-name> 
     <servlet-name>cr</servlet-name> 
    </filter-mapping> 

_ UPDATE _ _

我開始使用放大做我的請求,因爲我想確保它不是鍍鉻加上。我現在得到了400。以下是我的服務器上顯示的錯誤。

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.cr.entity.Products out of START_ARRAY token 
at [Source: [email protected]; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.cr.entity.Products out of START_ARRAY token 
at [Source: [email protected]; line: 1, column: 1 

這裏是放大定義和請求。

amplify.request.define("addRequest", "ajax", { 
      url: "service/products/addProduct", 
      type: "POST", 
      dataType: 'json', 
      contentType: 'application/json' 
     }); 
     amplify.request({ 
      resourceId: "addRequest", 
      data: JSON.stringify(jsonData), 
      success: function() { 
       alert("success") 
      }, 
      error: function() { 
       alert("fail") 
      } 
     }); 

數據:

var jsonData = [{ 
    "productId": 4, 
    "productModel": "Product Model 2", 
    "productName": "Product Name 2", 
    "dateAdded": 1361880001000, 
    "productWeight": 2, 
    "productStatus": "Hidden", 
    "productTaxClass": { 
     "taxId": 2, 
     "taxClassTitle": "High Tax Class", 
     "taxClassDescription": "This is a high tax class", 
     "lastModified": 1361880001000, 
     "dateAdded": 1361880001000 
    } 
}]; 
+2

您可以檢查您的客戶端發送正確的Content-Type頭,應用/ JSON – gouki 2013-03-01 02:41:43

+2

Spring MVC的依賴於傑克遜(或者1或者2)爲了處理JSON輸入或輸出。你的classpath中有沒有jackson庫? – 2013-03-01 06:56:10

+0

發佈您提交數據的客戶端代碼,問題似乎在客戶端 – Kris 2013-03-01 10:34:08

回答

5

我需要添加以下到jsonConverter豆。

<property name="prefixJson" value="false"/> 

最後一個bean是如下

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
     <property name="prefixJson" value="false"/> 
     <property name="supportedMediaTypes" value="application/json"/> 
    </bean> 

Another Stack Overflow Article