本文介绍了如何使用Spring HATEOAS更改HAL链接格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring HATEOAS(0.16.0.RELEASE)构建Spring REST应用程序,我希望JSON链接输出如下所示:

_links: {
   self: {
     href: "https://<ip>/api/policies/321"
   }
}

其呈现方式如下:

   "links":
      [{
       "rel":"self",
       "href":"http://<ip>/api/policies/321"
      }]

我正在使用HATEOAS ResourceResourceAssembler.

为什么我得到这种格式而不是其他格式?我该如何更改?

解决方案
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = { HypermediaType.HAL })
@ComponentScan(basePackages = {
        "com.jiwhiz.rest"
})
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer c) {
        c.defaultContentType(MediaTypes.HAL_JSON);
    }
}

引用:由Yuan Ji设计和构建带有Spring HATEOAS的RESTful API

I'm building a Spring REST application using Spring HATEOAS (0.16.0.RELEASE) and I'd like the JSON links output to look like:

_links: {
   self: {
     href: "https://<ip>/api/policies/321"
   }
}

while it renders like:

   "links":
      [{
       "rel":"self",
       "href":"http://<ip>/api/policies/321"
      }]

I'm using HATEOAS Resource and ResourceAssembler.

Why do I get this format instead of the other? How can I change it?

解决方案
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = { HypermediaType.HAL })
@ComponentScan(basePackages = {
        "com.jiwhiz.rest"
})
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer c) {
        c.defaultContentType(MediaTypes.HAL_JSON);
    }
}

cite: Design and Build RESTful API with Spring HATEOAS by Yuan Ji

这篇关于如何使用Spring HATEOAS更改HAL链接格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 05:47