gpt4 book ai didi

java - 以内部类作为参数的Spring Controller

转载 作者:行者123 更新时间:2023-11-29 06:53:36 34 4
gpt4 key购买 nike

我有一个 Spring Controller ,其方法映射到请求:

@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 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

最佳答案

似乎你应该让你的内部类static这样它就可以在不引用外部类的任何对象的情况下被实例化(例如new GenerateAlbumParams.SlideParams()

关于java - 以内部类作为参数的Spring Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878907/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com