Solve the problem that the integration of SpringBoot into Eureka causes the return result to change from json to xml

  • 2021-10-25 06:44:33
  • OfStack

The integration of SpringBoot into Eureka causes the return result to change from json to xml

Solutions

Add to the requested Mapping


produces = {  " application/json;charset=UTF-8 "  }

For example:


@GetMapping(value = "/user-instance", produces = { "application/json;charset=UTF-8" })

The following are json and xml


@GetMapping(value = "/user-instance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@GetMapping(value = "/user-instance", produces = MediaType.APPLICATION_XML_VALUE)

Support return in JSON and XML formats

Sometimes projects require two return formats, so we just need to add jackson xml dependencies


<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-xml-provider</artifactId>
</dependency>

eureka startup failure returns xml information

1. Why use this

After starting eclipse as usual today, start eureka project, visit the corresponding eureka but return a lot of xml information, look at the console but no error, very strange situation, Du Niang can't even find relevant information, google has a similar problem, there is a prompt said in the configuration file to add 1 line configuration, I tried still failed

2. Solutions

After trying various methods without success, I decided to start with maven.

For the first time, find the warehouse path under your own maven configuration, (windows-preference-maven-user settings local repository)

Enter org/springframework, delete cloud folder (eclipse needs to be closed), then open the projects under eclipse and update 1, and start eureka again after completion. The problem is still unsolved, and all the problems are not cloud package.

For the second time, very cruelly, delete the whole springframework folder directly and download it again. Success. eureka access returns to normal.

To sum up, the problem should lie in a package under springframework (cloud has been excluded), which has not been cleared yet.


Related articles: