代码之家  ›  专栏  ›  技术社区  ›  Awadesh

org.springframework.http.InvalidMediaTypeException在Spring引导应用程序中

  •  0
  • Awadesh  · 技术社区  · 6 年前

    我的spring boot应用程序运行良好。突然我面临一个奇怪的问题。

    下面是StackTrace:

     Inside Global Exception Handler. Exception caught is {}
     org.springframework.http.InvalidMediaTypeException: Invalid mime type "text/xml; charset=UTF-8,application/xml": UTF-8,application/xml
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:452) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:760) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:115) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:85) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:662) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:387) ~[spring-web-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    at com.airtelbank.insurance.utils.RestUtils.postXmlRequest(RestUtils.java:100) ~[classes!/:1.0.5]
    at com.airtelbank.insurance.utils.RestUtils.postXmlRequest(RestUtils.java:122) ~[classes!/:1.0.5]
    at com.airtelbank.insurance.service.impl.vehicle.IDVServiceImpl.getIDVValue(IDVServiceImpl.java:116) ~[classes!/:1.0.5]
    at com.airtelbank.insurance.controller.vehicle.GIController.checkIDV(GIController.java:198) ~[classes!/:1.0.5]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_151]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_151]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_151]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_151]
    

    控制器中的请求映射:

    @RequestMapping(value = "v1/test", method = RequestMethod.GET, consumer ="application/json",produces = {"application/JSON"})
    

    2 回复  |  直到 6 年前
        1
  •  1
  •   Mithat Konuk    6 年前

    在发布代码时,您需要以content type=application/xml或text/xml的形式发布请求

    @RequestMapping(value = "/test",consumer ="application/xml or text/xml", produces = {"application/json"})
    

    之后,将请求头端内容类型更改为application/xml或text/xml

    @RequestMapping(value = "v1/test", method = RequestMethod.POST,consume="text/xml",produces="application/json")
    @ResponseBody
    public ResponseEntity<CustomObject> test(@RequestBody CustomXMLObject) {
        // do some logical things for example change to json
    
    return new ResponseEntity<CustomObject>(convertedJsonObject,HttpStatus.OK);
    }
    

    将此方法请求为post,并将标题内容类型设置为text/xml

        2
  •  0
  •   piet.t Charis A.    6 年前

    当您从第三方服务读取xml时,您应该正确地解析它,并在Rest端点上添加以下注释以将其转换为json。

    @RequestMapping(value = "v1/test", method = RequestMethod.GET, consumes = {"application/json"} ,produces = {"application/json"})
    

    这个 produces consumes 属性分别指定该端点生成的数据类型和该端点使用(接受)的数据类型。