Spring 以内部 class 作为参数的控制器
Spring Controller with inner class as a parameter
我有一个 Spring 控制器,其方法映射到请求:
@RequestMapping(value = "/album", method = RequestMethod.POST)
public void generateAlbum(GenerateAlbumParams params, HttpServletResponse response){
...
}
GenerateAlbumParams 是一个简单的 class:
@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 的内部 class:
@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 Status 500 - Request processing failed; nested exception is
org.springframework.beans.InvalidPropertyException: Invalid property
'slideParams[0]' of bean class
[xxx.GenerateAlbumParams]: Illegal
attempt to get property 'slideParams' threw exception; nested
exception is org.springframework.beans.NullValueInNestedPathException:
Invalid property 'slideParams' of bean class
[xxx.GenerateAlbumParams]: Could not
instantiate property type
[xxx.GenerateAlbumParams$SlideParams]
to auto-grow nested property path; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[xxx.GenerateAlbumParams$SlideParams]:
Is it an abstract class?; nested exception is
java.lang.InstantiationException:
xxx.GenerateAlbumParams$SlideParams
似乎你应该让你的内部 class static
这样它就可以在不引用外部 class 的任何对象的情况下被实例化(例如 new GenerateAlbumParams.SlideParams()
我有一个 Spring 控制器,其方法映射到请求:
@RequestMapping(value = "/album", method = RequestMethod.POST)
public void generateAlbum(GenerateAlbumParams params, HttpServletResponse response){
...
}
GenerateAlbumParams 是一个简单的 class:
@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 的内部 class:
@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 Status 500 - Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'slideParams[0]' of bean class [xxx.GenerateAlbumParams]: Illegal attempt to get property 'slideParams' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'slideParams' of bean class [xxx.GenerateAlbumParams]: Could not instantiate property type [xxx.GenerateAlbumParams$SlideParams] to auto-grow nested property path; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [xxx.GenerateAlbumParams$SlideParams]: Is it an abstract class?; nested exception is java.lang.InstantiationException: xxx.GenerateAlbumParams$SlideParams
似乎你应该让你的内部 class static
这样它就可以在不引用外部 class 的任何对象的情况下被实例化(例如 new GenerateAlbumParams.SlideParams()