2

could anyone tell me why i see only values not properties:value in my JSON. I am using below code

@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);
}

Also i am not seeing any links, any idea if i am missing any configuration, Here is the JSON output

{"links":[],"content":[{"links":[],"content":["1000","100",100,"ACTIVE","ARROW","New York",1424980115000,null,"NY",null,"New York"],"id":null},

{"links":[],"content":["10001","100",100,"ACTIVE","Pivotal","Massachusetts",1425013200000,null,"MA",null,"Massachusetts"],"id":null}],

"metadata":{"size":10,"totalElements":2,"totalPages":1,"number":0},"nextLink":null,"previousLink":null,"id":null}

And here is another way i did based on your explanation at

[How to correctly use PagedResourcesAssembler from Spring Data?

OrderResourceAssembler.java

public class OrderResourceAssembler extends 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 extends ResourceSupport

  1. OrderController.java

    public class OrderController {

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private OrderResourceAssembler orderAssembler;
    
    @Autowired
    private PagedResourcesAssembler<Order> pagedAssembler;
    

    @RequestMapping(method = RequestMethod.GET, value = "/search") @ResponseBody PagedResources search( @Valid OrderSearch search, @PageableDefault(page = 1, value = Integer.MAX_VALUE) @SortDefault(sort = "poNumber", direction = Sort.Direction.DESC) Pageable pageable) { Page orders = orderService.searchOrders(search, pageable); return pagedAssembler.toResource(orders, orderAssembler); } }

When i try to hit the url, repository query gets executed but i get ClassCastException

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.mycomp.it.channeltracker.model.Order at com.mycomp.it.eis.channeltracker.api.resource.OrderResourceAssembler.toResource(OrderResourceAssembler.java:8) at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:122) at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:93) at com.emc.it.eis.channeltracker.api.OrderController.search1(OrderController.java:89)

Not sure what the issue is, And here is my servlet config

<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" /> 

and bean config for OrderResourceAssembler

class="com.mycomp.it.eis.channeltracker.api.resource.OrderResourceAssembler"/>

could you please provide me your inputs on what's missing

Community
  • 1
  • 1
gnanesh
  • 321
  • 1
  • 3
  • 12
  • So, what does it show then? Sure that orders actually has elements? – Oliver Drotbohm Feb 27 '15 at 06:21
  • yes orders have elements and I have added the JSON output , also i tried to follow another approach based on your response, please let me know what the issue really is , am i missing anything – gnanesh Mar 02 '15 at 00:45
  • I found the issue with ClassCastException and why i was not seeing Order attributes, infact the @Query i had in the Repository class i was returning only few columns instead of all columns as we have one payload column which is a BLOB column and i didn't want to pull this as part of the query, so instead of Order.class it was returning a Object class, so how do return Order object without some columns as part of Page call – gnanesh Mar 02 '15 at 02:50

1 Answers1

0

Replace your OrderResourceAssembler class with this one:

@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;
    }
}
Flor Gadea
  • 81
  • 1
  • 6