在Spring-Hateoas中使用ResourceAssemblerSupport和PathVariables时出现IllegalArgumentException

时间:2016-04-07 14:04:52

标签: java spring-mvc spring-hateoas spring-rest

我正在使用Spring-Hateoas和Spring-Boot创建我的第一个RESTful API。到目前为止,一切看起来都很有希望,但是当我尝试使用toResource()方法(通过调用http://localhost:8080/myVariable/greeting)扩展ResourceAssemblerSupport的类中创建资源时,我得到一个IllegalArgumentException告诉我

  

没有足够的变量值可用于根据原因扩展' var']

只有在类级别引用@PathVariable时才会发生这种情况,据我所知应该支持。

我的ControllerClass:

    @RestController
    @ExposesResourceFor(GreetingResource.class)
    @RequestMapping("/{var}")
    public class GreetingController {

        @Autowired
        private BeanFactory beanFactory;
        @Autowired
        private GreetingAssembler greetingAssembler;

        private static final String TEMPLATE = "Hello, %s!";

        @ResponseStatus(HttpStatus.OK)
        @RequestMapping("/greeting")
        public GreetingResource greeting( @PathVariable("var") String var ) {

            GreetingEntity greetingEntity = beanFactory.getBean(GreetingEntity.class, String.format(TEMPLATE, var));
            GreetingResource greeting = greetingAssembler.toResource(greetingEntity);
            greeting.add(linkTo(methodOn(GreetingController.class).greeting(var)).withSelfRel());

            return greeting;
        }
    }

我的ResourceAssembler类似乎有问题

    @Service
    public class GreetingAssembler extends ResourceAssemblerSupport<GreetingEntity, GreetingResource> {

        @Autowired
        private BeanFactory beanFactory;

        /**
         * constructor must call super to define which controller to link to
         * and to define the resource
         */
        public GreetingAssembler() {
            super(GreetingController.class, GreetingResource.class);
        }


        /**
         * based on configuration, build the resource
         *
         * @param entity
         * @return
         */
        @Override
        public GreetingResource toResource(GreetingEntity entity) {

            GreetingResource resource = createResourceWithId( entity.getId(),
                                                      entity);

            return resource;
        }

        /**
         * same as the method in ResourceAssemblerSupport, overwritten to evaluate the problem
         *
         * @param id
         * @param entity
         * @param parameters
         * @return
         */
        @Override
        protected GreetingResource createResourceWithId(Object id, GreetingEntity entity, Object... parameters) {

            Assert.notNull(entity);
            Assert.notNull(id);

            GreetingResource instance = instantiateResource(entity);
            ControllerLinkBuilder builder = linkTo(GreetingController.class, parameters);
            builder = builder.slash(id);
            Link link = builder.withSelfRel();
            instance.add(link);
            return instance;
        }

        /**
         * overwrite the default method to be able to call the class from aplicationcontext with params
         *
         * @param entity
         * @return
         */
        @Override
        protected GreetingResource instantiateResource(GreetingEntity entity) {

            return beanFactory.getBean(GreetingResource.class, entity.getContent());
        }
    }

GreetingResource和GreetingEntity是简单的POJO。

整个源代码位于https://github.com/r0bse/Spring-Hateoas-ResourceAssemblerSupport-Problem,基于一个简单示例:https://github.com/spring-guides/gs-rest-hateoas

我的问题是:我做错了什么,这是一个错误,还是我理解一切如何一起工作不够好?

0 个答案:

没有答案