2013-02-28 18 views
6

Nie mogę ustalić, dlaczego nie mogę publikować aktualizacji do kontrolera. Próbuję przesłać dane json za pomocą dodatku chrome. W końcu będę używał kątowego dla żądań. Sprawdziłem na innych artykułach stackoverflow i wydaje mi się, że mam wszystko, co sugerują.Aplikacja Spring MVC nie akceptująca JSON

Za to, co jest warte, mam żądanie GET do tego samego kontrolera, który działa bez problemu.

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. 

Mój dziennik serwera pokazuje poniższy

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) 

Posta do rozwiązania

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

danych jest wysłana mapowanie

{ 
    "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 
    } 
} 

Controller

@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 _

zacząłem używać wzmacniać zrobić moje prośby, bo chciałem mieć pewność, że to nie był chrom dodatek. Dostaję teraz 400. Poniżej znajduje się błąd wyświetlany na moim serwerze.

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 

Oto definicja i żądanie wzmocnienia.

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") 
      } 
     }); 

danych:

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

można sprawdzić, czy klient jest wysłanie odpowiedniego nagłówka Content-Type, application/json – gouki

+2

Wiosna MVC zależy Jackson (albo 1 lub 2) w celu obsługi wejścia lub wyjścia JSON. Czy posiadasz bibliotekę jackson w swojej ścieżce klas? –

+0

Po przesłaniu kodu po stronie klienta, problem wydaje się znajdować po stronie klienta. – Kris

Odpowiedz

5

Musiałem dodać następujące fasoli jsonConverter.

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

Finał Bean był następujący

<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