5

I want to disable HAL format by default.

Flow the document, I set this property.

spring.hateoas.use-hal-as-default-json-media-type=false

Then test it:

1. Request with header Accept: application/json.

Header: Content-Type: application/json;charset=UTF-8

Response:

{
  "links": {}
}

2. Request with header Accept: */* or without Accept header.

Header: Content-Type: application/hal+json;charset=UTF-8

Response:

{
  "_embedded": {},
  "_links": {}
}

The version of spring-boot is 1.5.3, and I use spring-boot-starter-hateoas.

Is anyway let hateoas to response json without HAL when no header specified?

Thanks a lot.


Update:

Here is a example base on the guide from spring.io.

Requests at: http://localhost:8080/greeting

Requests with header Accept: */*.

HTTP/1.1 200 
Content-Type: application/hal+json;charset=UTF-8

{
  "content":"Hello, World!",
  "_links":{
    "self":{
      "href":"http://localhost:8080/greeting?name=World"
    }
  }
}

Requests with header Accept: application/json.

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8

{
  "content":"Hello, World!",
  "_links":{
    "self":{
      "href":"http://localhost:8080/greeting?name=World"
    }
  }
}

I don't want HAL by default, so I add a config file resources/application.properties to the project.

spring.hateoas.use-hal-as-default-json-media-type=false

And test again.

Requests with header Accept: */*.

HTTP/1.1 200 
Content-Type: application/hal+json;charset=UTF-8

{
  "content":"Hello, World!",
  "_links":{
    "self":{
      "href":"http://localhost:8080/greeting?name=World"
    }
  }
}

Requests with header Accept: application/json.

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8

{
  "content":"Hello, World!",
  "links":[
    {
      "rel":"self",
      "href":"http://localhost:8080/greeting?name=World"
    }
  ]
}
RAM
  • 2,257
  • 2
  • 19
  • 41
FalconIA
  • 91
  • 1
  • 7
  • To make it easier for people to help you, can you provide a [minimal, complete, and verifiable example](/help/mcve) for your problem? – Andy Wilkinson Nov 03 '17 at 07:31
  • @AndyWilkinson OK, here is a example. – FalconIA Nov 06 '17 at 14:47
  • Unfortunately, that's not the sort of example I was looking for. As described on the page that I linked to, I was looking for some code that can easily be run to reproduce the problem. I link to a repository on GitHub would be ideal. – Andy Wilkinson Nov 06 '17 at 15:09
  • @AndyWilkinson The code was noticed in the doc. 1) Download and unzip the source repository for this guide, or clone it using Git: git clone https://github.com/spring-guides/gs-rest-hateoas.git 2) cd into gs-rest-hateoas/initial , run gradlew or mvnw 3) When you’re finished, you can check your results against the code in gs-rest-hateoas/complete. – FalconIA Nov 07 '17 at 02:51
  • @AndyWilkinson Excuse me, is there any result? – FalconIA Nov 13 '17 at 06:15
  • You've misunderstood the property. It has no affect when you're accepting */*. It just controls whether application/hal+json _may_ be served if you ask for application/json – Andy Wilkinson Nov 13 '17 at 17:08

0 Answers0