Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring Hateoas中使用ResourceAssemblerSupport和PathVariables时出现IllegalArgumentException_Java_Spring Mvc_Spring Hateoas_Spring Rest - Fatal编程技术网

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

Java 在Spring Hateoas中使用ResourceAssemblerSupport和PathVariables时出现IllegalArgumentException,java,spring-mvc,spring-hateoas,spring-rest,Java,Spring Mvc,Spring Hateoas,Spring Rest,我正在用SpringHateOAS和SpringBoot创建我的第一个RESTfulAPI。到目前为止,一切看起来都很有希望,但是当我尝试在一个类中创建资源,该类通过使用toResource()方法(通过调用)扩展ResourceAssemblerSupport时,我得到一个IllegalArgumentException告诉我 没有足够的变量值可用于扩展具有根本原因的“var”] 仅当我在类级别上引用@PathVariable时才会发生这种情况,据我所知,应该支持@PathVariable 我

我正在用SpringHateOAS和SpringBoot创建我的第一个RESTfulAPI。到目前为止,一切看起来都很有希望,但是当我尝试在一个类中创建资源,该类通过使用toResource()方法(通过调用)扩展ResourceAssemblerSupport时,我得到一个IllegalArgumentException告诉我

没有足够的变量值可用于扩展具有根本原因的“var”]

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

我的控制器类:

    @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());
        }
    }
@服务
公共类GreetingAssembler扩展了ResourceAssembler支持{
@自动连线
私人豆厂豆厂;
/**
*构造函数必须调用super来定义链接到哪个控制器
*以及定义资源
*/
公共问候汇编程序(){
super(GreetingController.class、GreetingResource.class);
}
/**
*基于配置,构建资源
*
*@param实体
*@返回
*/
@凌驾
公共问候资源存储资源(问候实体){
GreetingResource=createResourceWithId(entity.getId(),
实体);
返回资源;
}
/**
*与ResourceAssemblerSupport中的方法相同,被覆盖以评估问题
*
*@param-id
*@param实体
*@param参数
*@返回
*/
@凌驾
受保护的GreetingResource createResourceWithId(对象id、GreetingEntity实体、对象…参数){
Assert.notNull(实体);
Assert.notNull(id);
GreetingResource实例=实例化资源(实体);
ControllerLinkBuilder=linkTo(GreetingController.class,参数);
builder=builder.slash(id);
Link=builder.withSelfRel();
实例。添加(链接);
返回实例;
}
/**
*覆盖默认方法,以便能够使用参数从aplicationcontext调用类
*
*@param实体
*@返回
*/
@凌驾
受保护的GreetingResource实例化资源(GreetingEntity){
返回beanFactory.getBean(GreetingResource.class,entity.getContent());
}
}
GreetingResource和GreetingEntity是简单的POJO

整个源代码可在上获得,并基于一个简单的示例:


我的问题是:我是否做错了什么,是一个bug还是我对所有东西如何协同工作的理解不够好?

“methodOn(GreetingController.class)”缺少
var
的参数。感谢您的输入,但它之前已经失败了一行(在调用toResource()方法,然后调用createResourceWithId()时)在GreetingAssembler)中,同样的问题是:调用
createResourceWithId
,而不使用path变量的参数。谢谢!多亏了你的评论,我找到了这个解决方案:最后我只是通过了PathVariable,一切都按预期进行。