Spring HATEOAS PagedResource 未在 JSON 输出中显示属性或任何链接
Posted
技术标签:
【中文标题】Spring HATEOAS PagedResource 未在 JSON 输出中显示属性或任何链接【英文标题】:Spring HATEOAS PagedResource not displaying properties or any links in JSON Output 【发布时间】:2015-04-29 09:00:22 【问题描述】:谁能告诉我为什么我在 JSON 中只看到值而不是属性:值。我正在使用下面的代码
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/search")
PagedResources<Resource<Order>> search(@Valid OrderSearch search,
@SortDefault(sort = "poNumber", direction = DESC) Pageable pageable)
Page<Order> orders = orderService.searchOrders(search, pageable);
return pagedAssembler.toResource(orders);
我也没有看到任何链接,如果我缺少任何配置,我知道这是 JSON 输出
"links":[],"content":["links":[],"content":["1000","100",100,"ACTIVE","ARROW","New York",1424980115000,null,"NY",null,"New 约克"],"id":null,
"links":[],"content":["10001","100",100,"ACTIVE","Pivotal","Massachusetts",1425013200000,null,"MA",null,"Massachusetts "],"id":null],
"元数据":"size":10,"totalElements":2,"totalPages":1,"number":0,"nextLink":null,"previousLink":null,"id":null
这是我根据您在
处的解释所做的另一种方式[How to correctly use PagedResourcesAssembler from Spring Data?
OrderResourceAssembler.java
公共类 OrderResourceAssembler 扩展 ResourceAssemblerSupport
public OrderResourceAssembler()
super(OrderController.class, OrderResource.class);
/**
* @see org.springframework.hateoas.ResourceAssembler#toResource(java.lang.Object)
*/
@Override
public OrderResource toResource(Order entity)
OrderResource orderResource = null;
if (entity != null)
orderResource = createResourceWithId(
entity.getPurchaseOrderNumber(), entity);
orderResource.setPurchaseOrderNumber(entity
.getPurchaseOrderNumber());
orderResource.setAmount(entity.getAmount());
orderResource.setEndUser(entity.getEndUser());
orderResource.setPartner(entity.getPartner());
orderResource.setPayload(entity.getPayload());
orderResource.setProcessStatus(entity.getProcessStatus());
orderResource.setQuoteNumber(entity.getQuoteNumber());
orderResource.setQuoteStatus(entity.getQuoteStatus());
orderResource.setReceivedDate(entity.getReceivedDate());
orderResource.setRegion(entity.getRegion());
orderResource.setSalesOrderNumber(entity.getSalesOrderNumber());
orderResource.setShipTo(entity.getShipTo());
orderResource.setShipToCustomer(entity.getShipToCustomer());
orderResource.setType(entity.getType());
return orderResource;
2。 OrderResource 扩展了 ResourceSupport
OrderController.java
公共类 OrderController
@Autowired
private OrderService orderService;
@Autowired
private OrderResourceAssembler orderAssembler;
@Autowired
private PagedResourcesAssembler<Order> pagedAssembler;
@RequestMapping(method = RequestMethod.GET, value = "/search") @ResponseBody 分页资源搜索( @Valid OrderSearch 搜索, @PageableDefault(page = 1, value = Integer.MAX_VALUE) @SortDefault(sort = "poNumber", direction = Sort.Direction.DESC) 可分页) 页面订单 = orderService.searchOrders(search, pageable); return pagedAssembler.toResource(orders, orderAssembler);
当我尝试点击 url 时,存储库查询被执行但我得到 ClassCastException
java.lang.ClassCastException: [Ljava.lang.Object;不能转换为 com.mycomp.it.channeltracker.model.Order at com.mycomp.it.eis.channeltracker.api.resource.OrderResourceAssembler.toResource(OrderResourceAssembler.java:8) 在 org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:122) 在 org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:93) 在 com.emc.it.eis.channeltracker.api.OrderController.search1(OrderController.java:89)
不确定是什么问题,这是我的 servlet 配置
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
<property name="customArgumentResolvers">
<list>
<bean class="org.springframework.data.web.PageableArgumentResolver" />
</list>
</property>
</bean>
<bean id= "conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean class="org.springframework.data.web.PagedResourcesAssembler">
<constructor-arg>
<bean class="org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver" />
</constructor-arg>
<constructor-arg>
<null />
</constructor-arg>
</bean>
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />
OrderResourceAssembler 的 bean 配置
class="com.mycomp.it.eis.channeltracker.api.resource.OrderResourceAssembler"/>
能否请您提供关于缺少的内容的意见
【问题讨论】:
那么,它显示了什么?确定订单确实有元素? 是的订单有元素,我已经添加了 JSON 输出,我也尝试根据您的回复采用另一种方法,请让我知道问题到底是什么,我错过了什么 我发现了 ClassCastException 的问题以及为什么我没有看到 Order 属性,事实上我在 Repository 类中的@Query 我只返回了几列而不是所有列,因为我们有一个有效负载列是一个 BLOB 列,我不想把它作为查询的一部分,所以它不是 Order.class 它返回一个 Object 类,所以如何在没有某些列的情况下返回 Order 对象作为 Page用这个替换你的 OrderResourceAssembler 类:
@Component
public class OrderResourceAssembler implements ResourceAssembler<Order, Resource<Order>>
@Autowired EntityLinks entityLinks;
@Override
public Resource<Order> toResource(Order order)
Resource<Order> resource = new Resource<Order>(order);
final LinkBuilder lb =
entityLinks.linkForSingleResource(Order.class, order.getId());
resource.add(lb.withSelfRel());
// here is where you have to add you classes represented as links, like partner, endUser, etc.
resource.add(lb.slash("linkOne").withRel("linkOne"));
resource.add(lb.slash("linkTwo").withRel("linkTwo"));
return resource;
【讨论】:
以上是关于Spring HATEOAS PagedResource 未在 JSON 输出中显示属性或任何链接的主要内容,如果未能解决你的问题,请参考以下文章
Spring HATEOAS 与 Spring Data Rest
如何删除 Spring HATEOAS 中的“_embedded”属性