如何解决HTTP错误415-不支持的媒体类型

时间:2018-08-15 03:40:05

标签: java restlet

我正在使用Restet,我想知道是否可以设置ServerResource实体类型,例如这种类型的实体:

@XStreamAlias("role")
@ApiModel
public class Role {

    private String entityId;
    private String name;

    @ApiModelProperty(required = false, value = "")
    private List<String> aclRead;
    @ApiModelProperty(required = false, value = "")
    private List<String> acLWrite;
    @ApiModelProperty(required = false, value = "")
    private Boolean publicRead;
    @ApiModelProperty(required = false, value = "")
    private Boolean publicWrite;

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
        this.entityId = entityId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getAclRead() {
        return aclRead;
    }

    public void setAclRead(List<String> aclRead) {
        this.aclRead = aclRead;
    }

    public List<String> getAcLWrite() {
        return acLWrite;
    }

    public void setAcLWrite(List<String> acLWrite) {
        this.acLWrite = acLWrite;
    }

    public Boolean getPublicRead() {
        return publicRead;
    }

    public void setPublicRead(Boolean publicRead) {
        this.publicRead = publicRead;
    }

    public Boolean getPublicWrite() {
        return publicWrite;
    }

    public void setPublicWrite(Boolean publicWrite) {
        this.publicWrite = publicWrite;
    }

}

我们如何能够在无需传递所有字段的情况下进行POST,在此示例中,我们只想传递客户端请求中的“名称”,但是这样做会抛出415 - Unsupported Media Type

在我们的客户端中,我们仅传递此JSON

{
  "role" : {
     "name" : "AdminRole" 
  }
}

对于资源的Get响应,返回类型也是Role,因此客户端将通过Restlet编组获取所有字段,entityId,name,aclRead,aclWrite,publicRead和publicWrite。

我们遇到的问题是我们无法发布。

1 个答案:

答案 0 :(得分:0)

在发布请求中,json数据应仅由实体参数组成,而没有实体名称作为根键。因此,您的发帖请求应如下所示:

{
    "name" : "AdminRole" 
}