Spring无法构造接口模型的实例

时间:2018-07-31 16:32:24

标签: java spring model-view-controller interface model

当前正在尝试使产品收集数据,即在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");
  }

请忽略较差的缩进,因为它在堆栈上不太好。

1 个答案:

答案 0 :(得分:0)

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