【问题标题】:"How to change Rest response”“如何改变休息反应”
【发布时间】:2020-02-04 15:30:54
【问题描述】:

我正在尝试从我的 rest api 获取 json 响应,但我得到了这种格式!

我使用了@RestController 注释,我得到了“_embedded”!我不知道如何更改它,因为在我的前端应用程序中我不能使用这种格式!

      @RestController
      public class HeroRestController {


 @Autowired
 private HeroRepository HeroRepo;


 @GetMapping("/heroes")
    public List<Hero> getMessage(){
        return HeroRepo.findAll();
    }
}

我得到了什么:

    {
       "_embedded" : {
           "heroes" : [ {
              "name" : "hero11",
                "_links" : {
           "self" : {
       "href" : "http://localhost:8009/heroes/2"
        },
    "hero" : {
      "href" : "http://localhost:8009/heroes/2"
    }
  }
     }, {
      "name" : "hero22",
      "_links" : {
    "self" : {
      "href" : "http://localhost:8009/heroes/3"
    },
    "hero" : {
      "href" : "http://localhost:8009/heroes/3"
    }
  }
}

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    SPRING HATEOAS (HateoasProperties)

    使用spring.data.rest.defaultMediaType = application/json

    spring.hateoas.use-hal-as-default-json-media-type=trueapplication.properties 文件中。有关更多 Spring 数据休息 HATEOS ,请参阅 SPRING HATEOAS (HateoasProperties)

    【讨论】:

    • 我得到了这个格式:{ "links" : [ { "rel" : "self", "href" : "localhost:8009/heroes{?page,size,sort}", "hreflang" :空,“媒体”:空,“标题”:空,“类型”:空,“弃用”:空},{“rel”:“配置文件”,“href”:“localhost:8009/profile/heroes”,“hreflang”:空,“媒体”:空,“标题”:空,“类型”:空,“弃用”:空},{“rel”:“搜索”,“href”:“localhost:8009/heroes/search”,“hreflang”:空,“媒体”:空,“标题”:空,“类型”:空,“弃用”:空}],
    猜你喜欢
    • 1970-01-01
    • 2016-01-26
    • 2019-06-24
    • 2018-02-18
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多