2013-02-28 7 views
6

컨트롤러에 업데이트를 게시 할 수없는 이유를 알 수 없습니다. 크롬 애드온을 통해 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. 

내 서버 로그

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

컨트롤러 매핑

을 게시되고 해결하기 위해 다음과 같은

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) 

포스트를 보여줍니다

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

데이터 : I이 (가) jsonConverter 콩에 다음을 추가하는 데 필요한

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

을 다음과 같이

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

최종 콩이었다, 응용 프로그램/JSON – gouki

+2

스프링 MVC는 (중 1 잭슨에 따라 달라집니다 또는 2) JSON 입력 또는 출력을 처리하기 위해. 수업 중에 잭슨 도서관이 있나요? –

+0

데이터를 제출하는 클라이언트 측 코드를 게시하십시오. 문제가 클라이언트 측에있는 것 같습니다. – Kris

답변

5

. 클라이언트가 올바른 컨텐츠 유형 헤더를 보내는 경우 확인할 수 있습니다

<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

관련 문제