我有一个Spring控制器,其方法映射到请求:
@RequestMapping(value = "/album", method = RequestMethod.POST)
public void generateAlbum(GenerateAlbumParams params, HttpServletResponse response){
...
}
GenerateAlbumParams是一个简单的类:
@Getter
@Setter
public class GenerateAlbumParams {
public enum Position {LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM}
private List<SlideParams> slideParams = new ArrayList<>();
}
SlideParams是:
@Getter
@Setter
public class SlideParams{
private Boolean checked = false;
private Long id;
private GenerateAlbumParams.Position mapLegendPosition;
private GenerateAlbumParams.Position mapTablePosition;
}
一切正常,但是如果我想让SlideParams作为GenerateAlbumParams的内部类:
@Getter
@Setter
public class GenerateAlbumParams {
public enum Position {LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM}
private List<SlideParams> slideParams = new ArrayList<>();
@Getter
@Setter
public class SlideParams{
private Boolean checked = false;
private Long id;
private Position mapLegendPosition;
private Position mapTablePosition;
}
}
它引发异常:
HTTP状态500-请求处理失败;嵌套异常为
org.springframework.beans.InvalidPropertyException:无效的属性
Bean类的“ slideParams [0]”
[xxx.GenerateAlbumParams]:非法
试图获取属性“ slideParams”引发异常;嵌套的
异常是org.springframework.beans.NullValueInNestedPathException:
Bean类的无效属性“ slideParams”
[xxx.GenerateAlbumParams]:无法
实例化属性类型
[xxx.GenerateAlbumParams $ SlideParams]
自动增长嵌套的属性路径;嵌套异常为
org.springframework.beans.BeanInstantiationException:失败
实例化
[xxx.GenerateAlbumParams $ SlideParams]:
它是抽象类吗?嵌套异常为
java.lang.InstantiationException:
xxx.GenerateAlbumParams $ SlideParams
最佳答案
似乎您应该将内部类设为static
,以便可以实例化它而无需引用任何外部类的对象(例如new GenerateAlbumParams.SlideParams()