Spring HATEOAS 不遵守资源 link 的默认包含 属性
Spring HATEOAS does not respect default inclusion property for resource link
我遇到的问题与 this question 中提出的问题类似,但是,应用建议的解决方案
spring.jackson.default-property-inclusion=NON_NULL
不会阻止 HATEOAS 呈现具有空属性的链接。这是我的控制器声明
@RestController
@ExposesResourceFor(Customer.class)
public class CustomerController {
// controller methods here
}
和网络配置 class
@Configuration
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class DataApiWebConfiguration extends WebMvcConfigurationSupport {
// config here
}
在 return 作为资源的控制器获取方法中,我声明映射如下
@GetMapping(value = "/customers/{id}", produces = MediaTypes.HAL_JSON_VALUE)
然后我return一个Resource
Optinal<Customer> customer = customerRepository.findById(id);
return customer.map(customerResourceAssembler::toResource).map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
CustomerResourceAssembler
扩展了 SimpleIdentifiableResourceAssembler
,如 this spring-hateaos example 所示。
但在响应正文中,我仍然看到使用空属性呈现的链接
"links": [
{
"rel": "self",
"href": "http://localhost:8080/customers/11",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
}
]
这看起来不像是 HATEOAS 的响应,就像我在 JSON
中看到的 _links
而不是 links
的示例一样
问题出在我的配置 class,我以错误的方式注册了 Hibernate5Module,删除了这些行
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new HibernateAwareObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
并简单地添加了一个 bean
@Bean
public Module hibernate5Module() {
return new Hibernate5Module();
}
修复了输出
{
"_embedded": {
"customers": [
{
"name": "Minty And Sons Pvt. Ltd.",
"pan": "5GB7W15M0T",
"currecny": "INR",
"tds": 0.1,
"invoice_prefix": "INV",
"_links": {
"self": {
"href": "/customers/1"
},
"customers": {
"href": "/customers"
},
"contact": {
"href": "/customers/1/contact"
},
"branches": {
"href": "/customers/1/branches"
},
"invoices": {
"href": "/customers/1/invoices"
},
"paid-invoices": {
"href": "/customers/1/invoices/paid"
},
"pending-invoices": {
"href": "/customers/1/invoices/pending"
},
"overdue-invoices": {
"href": "/customers/1/invoices/overdue"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/data/api/customers"
}
},
"page": {
"size": 20,
"total_elements": 1,
"total_pages": 1,
"number": 0
}
}
我遇到的问题与 this question 中提出的问题类似,但是,应用建议的解决方案
spring.jackson.default-property-inclusion=NON_NULL
不会阻止 HATEOAS 呈现具有空属性的链接。这是我的控制器声明
@RestController
@ExposesResourceFor(Customer.class)
public class CustomerController {
// controller methods here
}
和网络配置 class
@Configuration
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class DataApiWebConfiguration extends WebMvcConfigurationSupport {
// config here
}
在 return 作为资源的控制器获取方法中,我声明映射如下
@GetMapping(value = "/customers/{id}", produces = MediaTypes.HAL_JSON_VALUE)
然后我return一个Resource
Optinal<Customer> customer = customerRepository.findById(id);
return customer.map(customerResourceAssembler::toResource).map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
CustomerResourceAssembler
扩展了 SimpleIdentifiableResourceAssembler
,如 this spring-hateaos example 所示。
但在响应正文中,我仍然看到使用空属性呈现的链接
"links": [
{
"rel": "self",
"href": "http://localhost:8080/customers/11",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
}
]
这看起来不像是 HATEOAS 的响应,就像我在 JSON
中看到的_links
而不是 links
的示例一样
问题出在我的配置 class,我以错误的方式注册了 Hibernate5Module,删除了这些行
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new HibernateAwareObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
并简单地添加了一个 bean
@Bean
public Module hibernate5Module() {
return new Hibernate5Module();
}
修复了输出
{
"_embedded": {
"customers": [
{
"name": "Minty And Sons Pvt. Ltd.",
"pan": "5GB7W15M0T",
"currecny": "INR",
"tds": 0.1,
"invoice_prefix": "INV",
"_links": {
"self": {
"href": "/customers/1"
},
"customers": {
"href": "/customers"
},
"contact": {
"href": "/customers/1/contact"
},
"branches": {
"href": "/customers/1/branches"
},
"invoices": {
"href": "/customers/1/invoices"
},
"paid-invoices": {
"href": "/customers/1/invoices/paid"
},
"pending-invoices": {
"href": "/customers/1/invoices/pending"
},
"overdue-invoices": {
"href": "/customers/1/invoices/overdue"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/data/api/customers"
}
},
"page": {
"size": 20,
"total_elements": 1,
"total_pages": 1,
"number": 0
}
}