首页 > 解决方案 > 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");
  }

请忽略可怜的缩进,我在堆栈上不太擅长。

标签: javaspringmodel-view-controllerinterfacemodel

解决方案


在您的控制器中,有一行尝试创建此接口 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!!

推荐阅读