![](/img/trans.png)
[英]Java RESTFUL-Service returns 500 Internal Server Error (jersey)
[英]500 Internal server error - Jersey (Java web service)
我有一個Java REST服務,它為客戶端(使用Grizzly和Jersey)提供了幾種CRUD服務。 我一直在尋找這個問題好幾天了,但我真的不明白,因為服務器提供了另一個調用,該調用幾乎是相同的,並且工作正常。
這是不起作用的服務的代碼片段:
@Path("objets")
public class ObjetResource {
@PUT
@Path("/{code}")
@Consumes(MediaType.APPLICATION_JSON)
public void updateObject(
@PathParam("code") String code,
final Objet updatedObject){
System.out.println("UpdateObject is called!");
}
}
應該使用以下URL調用此服務: http:// localhost:8080 / webservice / objets / newCode
這是一項有效的服務:
@Path("domaines")
public class DomaineResource {
@PUT
@Path("/{nom}")
@Consumes(MediaType.APPLICATION_JSON)
public void updateDomaine(
@PathParam("nom") String nom,
final Domaine updatedDomaine
){
System.out.println("UpdateDomaine is called!");
}
}
使用以下URL調用時,此服務有效: http:// localhost:8080 / webservice / domaines / newDomaine
不幸的是,我收到“ 500內部服務器錯誤” ...當然,“此函數被調用”卻從未顯示...):我試圖刪除整個“ updateObject”函數,當我這樣做時,錯誤變成“ 405方法不允許” D :!
您知道我為什么會遇到這個問題嗎?
有什么辦法可以獲取有關正在發生的錯誤的更多信息? 用這些小信息很難解決我的問題):
編輯 :我已經嘗試了幾種方法來糾正我的問題。 首先,我嘗試簡化“ updateObjet”功能。 我注意到有些奇怪的地方:
當我發送以下json時:
{ "code" : "codeValue" }
成功顯示“已調用UpdateObject”。 但是,如果我發送
{ "code" : "codeValue", "type" : "platform.field" }
什么都沒有顯示。
這是我的Objet類的代碼:
package classes;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Objet extends Model{
@XmlElement(name="code")
private String code;
@XmlElement(name="type")
private String type;
@XmlElement(name="parent")
private String parent;
@XmlElement(name="annotations")
private String annotations;
@XmlElement(name="ontologyuri")
private String ontologyuri;
@XmlElement(name="access")
private String access;
public Objet(){
}
public Objet(String code, String type, String parent, String annotations, String ontologyuri, String access){
this.code = code;
this.type = type;
this.parent = parent;
this.annotations = annotations;
this.ontologyuri = ontologyuri;
this.access = access;
}
public void setCode(String code) {
this.code = code;
}
public String getCode(){
return this.code;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getAnnotations() {
return annotations;
}
public void setAnnotations(String annotations) {
this.annotations = annotations;
}
public String getOntologyuri() {
return ontologyuri;
}
public void setOntologyuri(String ontologyuri) {
this.ontologyuri = ontologyuri;
}
public String getAccess(){
return access;
}
public void setAccess(String access) {
this.access = access;
}
public String toString(){
return "Code : " + getCode() + " Type : " + getType() + " Parent : " + getParent() + " Annotations : " + getAnnotations() + " ontologyuri : " + getOntologyuri() + " access : " + getAccess();
}
}
這是我的類Domaine的代碼:
package classes;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Domaine extends Model{
@XmlElement(name="nom")
private String nom;
@XmlElement(name="adresse")
private String adresse;
@XmlElement(name="description")
private String description;
@XmlElement(name="access")
private String access;
@XmlElement(name="plateformes")
private ArrayList<Plateforme> plateformes;
public Domaine(){}
public Domaine(String nom, String adresse, String description, String access){
this.nom = nom;
this.adresse = adresse;
this.description = description;
this.access = access;
}
public Domaine(String nom, String adresse, String description, String access, ArrayList<Plateforme> plateformes){
this.nom = nom;
this.adresse = adresse;
this.description = description;
this.access = access;
this.plateformes = plateformes;
}
public String getNom(){
return this.nom;
}
public void setNom(String nom){
this.nom = nom;
}
public String getAdresse(){
return this.adresse;
}
public void setAdresse(String adresse){
this.adresse = adresse;
}
public String getDescription(){
return this.description;
}
public void setDescription(String desc){
this.description = desc;
}
public String getAccess(){
return this.access;
}
public void setAccess(String access){
this.access = access;
}
//Manipulation des plateformes
public ArrayList<Plateforme> getPlateformes(){
return this.plateformes;
}
public void addPlateforme(Plateforme p){
this.plateformes.add(p);
}
public void removePlateforme(Plateforme p){
this.plateformes.remove(p);
}
public String toString(){
return "Nom : " + getNom() + " Adresse : " + getAdresse() + " Description : " + getDescription() + " Access " + getAccess();
}
}
編輯2 :我一直在嘗試理解我的錯誤,並添加了一些可能有所幫助的東西:首先,我注意到當我在http:// localhost:8080 / webservice /上執行“ GET”操作時domaines ,我收到以下JSON:
[{"type":"domaine","nom":"DomaineTest0","adresse":"domaine de test 0","description":"","access":"test"},
{"type":"domaine","nom":"DomaineTest1","adresse":"Domaine de test 1","description":""}]
如您所見,有一個“類型”字段,在我的類Domaine中未指定。 我已經在Jersey的文檔中進行了一些搜索,但是目前我不知道此“類型”字段的來源。 此信息有趣的是,我指定的Objet類具有一個“類型”字段。 我在想,也許這個神秘出現的“類型”字段正在干擾我的Objet的“類型”字段?
500 Internal server error
僅表示500 Internal server error
中已引發某些異常,並且請求未按預期完成。 所以我想它可能是一些null
指針異常,原因可能是無效的JSON
或我不確定的代碼邏輯。
你可以做的事
JSON
,而不是 這些只是瘋狂的猜測,我認為這可能對您有所幫助!
由於這篇文章而解決了問題: 從討論類似問題的JSON輸出球衣moxy中刪除“類型” 。
我應用了Dennis Mitchell的解決方案,該解決方案使用@XmlType(name="")
注釋了我的類Objet,這是必需的,因為Objet是一個子類。 但是,作為丹尼斯,我不確定為什么這樣做。
謝謝大家的幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.