Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My spring boot application was running fine. Suddenly I am facing an strange issue.

Below is the 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]

Request Mapping in controller:

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

Any Help is appreciated.

i think this is about when you request on controller dispatcher servlet is mapping via consumer and producer type I think you need to change request mapping consumer type maybe controller wants as a json but you sending as text / xml . I think you using postman right ? – Mithat Konuk Aug 29, 2018 at 11:16 Actually it was working fine , since last one hour i am getting this exception. It is throwing exception inside the Spring boot application when i am trying to access them – Awadesh Aug 29, 2018 at 11:17 You should add your basic details about your issue in question instead of comments on answer. – Sangam Belose Aug 29, 2018 at 12:00

as you post code you need to post your request as content-type = application/xml or text/xml

change like this

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

after that change your request header side content-type as application/xml or text/xml then it will be fixed.And also provide method (GET ,POST)

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

request this method as post and set header content-type as text/xml

I am consuming SOAP service of third party i.e. xml request and response. I am parsing xml response from third party and sending JSON response from my service – Awadesh Aug 29, 2018 at 11:25

When you are reading the xml from 3rd party service, you should parse it correctly and have below annotation on your Rest endpoint to convert it into json.

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

The produces and consumes attributes specifies Type of data produced by that endpoint and type of data consumed (accepted) by that endpoint respectively.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.