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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28185713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:09:44  来源:igfitidea点击:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/xml;charset=UTF-8' not supported

javaspringutf-8

提问by Nishant Prabhu

I am getting below exception when I am calling rest service.

当我调用休息服务时,我遇到了异常。

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

org.springframework.web.HttpMediaTypeNotSupportedException:org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:1522) 不支持内容类型“application/xml;charset=UTF-8” -webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] 在 org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:181) [spring-webmvc-4.1.1 .RELEASE.jar:4.1.1.RELEASE] 在

Code

代码

    //    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));

}

采纳答案by Nishant Prabhu

The issue has been resolved. The pom entry given below which was conflicting with Hymanson core libraries. Just removed it and it all worked fine.

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

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

回答by Bond - Java Bond

Your controller is implemented such as to accept only JSON values i.e. consumes = MediaType.APPLICATION_JSON_VALUE; thus the error which clearly states that XML is not supported.

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

In case XML is intended type update the controller to include MediaType.APPLICATION_XMLor MediaType.APPLICATION_XML_Value

如果打算使用 XML 类型更新控制器以包含MediaType.APPLICATION_XMLMediaType.APPLICATION_XML_Value