出現415 unsupprot media Type 錯誤和 InputStream close 錯誤的解決方法如下:
解決方法:由於cxf 對元素進行了封裝,它有一個註解叫作@FormParam和QueryParam,@FormParam 用於更新和添加,QueryParam 用於查詢。剛開始我還以爲是我的@Consume 接收的類型錯了,後來我發現了這個祕密,但是你的@Consumes 也必須寫正確,寫不對也報錯,寫錯了沒有加這個註解也報415錯誤,cxf 裏面的封裝沒有spring那麼牛,直接就給你弄成對象了,你直接寫個對象它會報錯的。
這是pojo的寫法
@Path("customers")
@XmlRootElement(name="studentVO")
public class StudentVO {
private String sNo;
@FormParam("sName")
private String sName;
@FormParam("sAge")
private Integer sAge;
@FormParam("sSex")
private String sSex;
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public Integer getsAge() {
return sAge;
}
public void setsAge(Integer sAge) {
this.sAge = sAge;
}
public String getsSex() {
return sSex;
}
public void setsSex(String sSex) {
this.sSex = sSex;
}
public String toJson() {
return JSON.toJSONString(this);
}
}
接下來這是接口的寫法
@GET
@Path(value ="/selectStudentBysNo2/{sNo}")
@Produces("application/json")
@Consumes({"text/plain","application/json","application/xml"})
StudentVO selectStudentVOBysNo2(@PathParam("sNo") String sNo);
@POST
@Path(value="/updateStudent")
@Produces("application/json")
@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
Response updateStudent(@FormParam("")StudentVO studentVO);
@POST
@Path(value="/deleteStudent")
String deleteString(@FormParam("sNo")String sNo);
@POST
@Path(value="/addStudent")
@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
Response addStudent(@FormParam("")StudentVO studentVO);
}