java - Spring无法构造接口模型的实例
问题描述
目前正在尝试做一个产品数据收集,即在一个spring mvc api中容纳三个不同类别的产品。我做了4个model.java;3 用于存储与这些类型中的每一个相对应的唯一数据。还有一个包含所有共同属性的第四个。最后我做了一个模型接口,用于根据操作决定使用哪个模型。
但是,在我创建我的 Create 方法时。我偶然发现“无法构造接口实例”错误。
抽象模型
public abstract class AbstractProduct implements Product {
@Getter private String productId;
@Getter @Setter private String productName;
@Getter @Setter private String duration;
@Getter @Setter private String productType;
@Getter @Setter private String productDescription;
@Getter @Setter private Calendar startDate;
@Getter @Setter private Calendar endDate;
public AbstractProduct(){
}
public AbstractProduct (final String productId,final String name, final String duration, final String type,
final String description, final Calendar startDate, final Calendar endDate) {
this.productId = productId;
this.productName = name;
this.duration = duration;
this.productType = type;
this.productDescription = description;
this.startDate = startDate;
this.endDate = endDate;
}
}
产品型号
public class SubscriptionProduct extends AbstractProduct implements Product{
@Getter @Setter private String subscriptionType;
@Getter @Setter private String renewelProductId;
public SubscriptionProduct(){}
@Builder
public SubscriptionProduct (final String productId, final String name, final String duration,
final String description, final Calendar startDate, final Calendar endDate,
final String subscriptionType,final String renewlProductId) {
super(productId, name, duration, "SUBSCRIPTION", description, startDate, endDate);
this.subscriptionType = subscriptionType;
this.renewelProductId = renewlProductId;
}
}
产品型号界面:
public interface Product {
}
我可以看到问题出在哪里,并且我很确定它在控制器中。但是我有点不确定如何解决这个问题以允许产品界面按预期工作。
控制器:
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity<Product> create (@RequestBody final Product data) {
final ObjectMapper mapper = new ObjectMapper();
Product result = null;
HttpStatus status = HttpStatus.OK;
try {
result = service.createProduct(data);
}catch(Exception e) {
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
if (result != null) {
return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(result, status);
}
有没有办法解决我遇到的这个问题?
编辑1:继承人我的服务实现类:
public Product createProduct(Product data) throws ProductException {
Product result = null;
ParameterCheckTool.check(new int[]{ParameterCheckTool.CHECK_TYPE_NULL},
new Object[]{data},
new String[]{"data"},
true,
"createProduct",
HostProductService.class);
checkIfServiceIsRunning(this);
ProductServiceHandler handler = findHandler("");
if (handler == null) {
throw new ProductException("No handler found");
}
result = handler.createProduct(data);
return result;
}
private ProductServiceHandler findHandler(String request) {
//Apply Logic here for selecting Handlers.
return handlers.get("TestHandler");
}
请忽略可怜的缩进,我在堆栈上不太擅长。
解决方案
在您的控制器中,有一行尝试创建此接口 Product 的实例,应该将其删除:
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity<Product> create (@RequestBody final Product data) {
final ObjectMapper mapper = new ObjectMapper();
Product result = null; //Remove this line!!
推荐阅读
- git - git log --cherry-pick --right-only --no-merges 会忽略分支之间正确挑选的所有提交吗?
- r - 将 .xlsx 文件保存到磁盘,形成 http post 请求
- python - 尝试索引字符串列表并根据其索引删除字符串
- java - 如果 Liquibase 因更改集迁移失败而失败,我的 csv 数据有什么问题...原因:java.lang.NullPointerException
- javascript - ajax 加载后,网站在 Chrome 中切换全屏模式
- python - 在 Python 菜单中扫描字符串文字时 EOL
- angular - 需要用于 mat-row Angular 的 Box 阴影
- java - POI API 的事件模型在 3.15 版本中是否不适用(出现编译错误)?
- observable - 如何添加可观察的集合项
- java - 为什么我不能从我的多维数组中打印字符串?