首页 > 解决方案 > java.lang.InstantiationException 第二次

问题描述

我这里有一个小问题,需要我一些意见。

这是代码的和平

@XmlRootElement
public abstract class Article {
    // quert params and name of xml elements
    private String name;
    private String author;
    private String description;
    private String picture_url;
    private double price;
    private int id;

    public Article() {
    }

    public Article(String name, String author, String description, String picture_url, double price, int id) {
        super();
        this.name = name;
        this.author = author;
        this.description = description;
        this.picture_url = picture_url;
        this.price = price;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public String getDescription() {
        return description;
    }

    public String getPicture_url() {
        return picture_url;
    }

    public double getPrice() {
        return price;
    }

    public int getId() {
        return id;
    }
}

而且我还有扩展文章的类 CD。问题是当我尝试在 REST 应用程序中创建带有 POST 请求的新文章时,每次都会抛出异常,但我也在 CD 类中提供了无 arg 构造函数?这里有什么问题?

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/articles")
public List<Article> gellAllItems(){
    return new ArrayList<>(repo.get().values());
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/articles/{articleId}")
public Article get(@PathParam("articleId") int id) {
    return repo.get(id);
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/articles")
public Article create(Article article)  {  // return a specific response with entity
    repo.create(article);
    return article;
}

标签: java

解决方案


你的课是abstract. 它不能被实例化。删除摘要,它应该可以工作。


推荐阅读