Shivraj
Shivraj

Reputation: 492

Springboot error for Eureka with browsers showing xml instead of json

I have created one microservice using Java8 and SpringBoot using Maven. Lets call it as MicroServiceA

It has controller which returns ResponseEntity object as below:

    @RestController
    @RequestMapping("/api")
    public class MicroserviceAController {  


        @GetMapping(value = "/all")
        public ResponseEntity<ServiceAResponseWrapper<List<ServiceADto>>> getAll() {

ServiceAResponseWrapper<List<ServiceADto>> wrapper = 
    new ServiceAResponseWrapper<List<ServiceADto>>(ServiceAResponseStatus.SUCCESS,findAll());

return new ResponseEntity<ServiceAResponseWrapper<List<ServiceADto>>>(wrapper,HttpStatus.OK);

        }

      public static List<ServiceADto> findAll() {
        //returns list of ServiceADto objects
    }

    }

When I start this service and verify it in any browser: http://localhost:8073/api/all/ , I get JSON response displayed.

Now if I want to introduce my service to EUREKA service registry then I will need to do following changes.

  1. Create EUREKA server microservice. I start it - http://localhost:8761/
  2. Make changes to MicroserviceA as follows -

Go to pom.xml and add dependency

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

go to application.yml and add this:

eureka:
  client:
    registerWithEureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/

As soon as I start my service then I start seeing it on server http://localhost:8761/

Now I again go to browser and try to check my microservice http://localhost:8073/api/all/ What I see is XML and not JSON.

I even tried to fix it by modifying my Microservice Controller by adding annotation to my method:

@Produces( { MediaType.APPLICATION_JSON} )

But with that also I see XML and not JSON.

Am I missing something or its normal behavior with EUREKA ? If yes, how do I fix it?

Upvotes: 2

Views: 2159

Answers (3)

ihaider
ihaider

Reputation: 1350

There are two potential solutions for this:

First: exclude the jackson-dataformat-xml dependency from all spring-cloud-starter-* artifacts if your application has nothing to do with XML conversions. One exclusion example from spring-cloud-starter-netflix-eureka-client is below. For my case, I had to exclude Jackson XML dependency from spring-cloud-starter-netflix-ribbon, spring-cloud-starter-openfeign and spring-cloud-starter-netflix-eureka-client

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
            </exclusion>
        </exclusions>
</dependency>

Second: If you want to support both XML and JSON responses. You can pass Accept: header with the request to your API.

For getting JSON response:

curl -X GET \
  http://localhost:8073/api/all/ \
  -H 'Accept: application/json' 

For getting XML response:

curl -X GET \
  http://localhost:8073/api/all/ \
  -H 'Accept: application/xml'

Upvotes: 1

Aswin Ravi
Aswin Ravi

Reputation: 315

If you are using older version of spring cloud starter, you might need to exclude Jackson dataformat XML dependency

      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Upvotes: 5

EliKnaffo
EliKnaffo

Reputation: 354

Hi I myself never used Eureka but from a quick search there is a ready to use API that converts the XML to json as Eureka uses XML and not json because json can’t hold attributes. Link to the site explaining how to do this -> https://automationrhapsody.com/json-format-register-service-eureka/amp/

Hope this helps you out

Upvotes: 0

Related Questions