org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application / xml; charset = UTF-8'

时间:2015-01-28 06:03:36

标签: java spring utf-8

当我打电话给休息服务时,我得到的是异常。

  

org.springframework.web.HttpMediaTypeNotSupportedException:Content   type' application / xml; charset = UTF-8'不支持                 at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:152)   [弹簧webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]                 在org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:181)   [弹簧webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]                 在

代码

    //    This method calls the rest service


        @Override
        public TransactionSearchResults callSearchTransactions(TransactionSearchCriteria criteria, int page, int size) {

        HttpEntity<TransactionSearchCriteria> requestEntity = new HttpEntity<TransactionSearchCriteria>(criteria, getCommonHeaders(new HttpHeaders()));

        Map<String, Integer> params = new HashMap<String, Integer>();
        params.put("size", size);
        params.put("page", page);

        return restTemplate.exchange(urlBase + "/transaction?size={size}&page={page}", HttpMethod.POST, requestEntity, TransactionSearchResults.class, params).getBody();

        }

    // Api which caters to rest call

        @Controller
        @RequestMapping("/transaction")
        public class TransactionStatusController extends BaseController { ... }

    //Controller method for rest call 
        @ResponseBody
        @RequestMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST , consumes = MediaType.APPLICATION_JSON_VALUE)
            public com.rest.TransactionSearchResults searchTransactions(@RequestParam(value = "page", required = false) Integer page,
// Using request Param to retireve criteria                         
@RequestParam(value = "size", required = false) Integer size, @Valid     @RequestBody com.rest.TransactionSearchCriteria criteria) {
// This gets relevant results and return it to rest call
    return convert(transactionService.search(convert(criteria), page, size));

}

2 个答案:

答案 0 :(得分:1)

您的控制器实现为仅接受JSON值,即consumes = MediaType.APPLICATION_JSON_VALUE;因此,错误明确表明不支持XML。

如果是XML,则类型更新控制器以包含MediaType.APPLICATION_XMLMediaType.APPLICATION_XML_Value

答案 1 :(得分:0)

问题已经解决。下面给出的pom条目与jackson核心库冲突。刚删除它,一切正常。

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.4.3</version>
</dependency>