500 内部服务器错误 - 泽西岛(Java 网络服务)

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 内部服务器错误”...当然 "This function is called" 永远不会显示...): 我尝试删除整个 "updateObject" 函数,当我这样做时,错误变为“405 方法不允许”D:!

你知道我为什么会遇到这个问题吗?

有什么方法可以获得有关发生的错误的更多信息?这么少的信息真的很难解决我的问题:

编辑: 我已经尝试了几件事来纠正我的问题。 首先,我尝试简化 "updateObjet" 函数。我注意到一些很奇怪的事情:

当我发送以下内容时 json:

{ "code" : "codeValue" }

"UpdateObject is called"显示成功。但是,如果我发送

{ "code" : "codeValue", "type" : "platform.field" }

没有显示。

这是我的 class 对象的代码:

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();
}

}

这是我的 class 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/domaines 上执行 "GET" 时,我收到以下 JSON:

[{"type":"domaine","nom":"DomaineTest0","adresse":"domaine de test 0","description":"","access":"test"},
{"type":"domaine","nom":"DomaineTest1","adresse":"Domaine de test 1","description":""}]

如您所见,有一个 "type" 字段,在我的 class Domaine 中没有指定。我在泽西岛的文档中搜索了一下,但目前我无法理解这个 "type" 字段的来源。 此信息的有趣之处在于我指定的 class 对象有一个 "type" 字段。我在想也许这个神秘出现的 "type" 字段干扰了我的 Objet 的 "type" 字段?

500 Internal server error 只是表示服务器抛出了一些异常,请求没有按预期完成。所以我猜它可能是一些 null 指针异常,原因可能是你的无效 JSON 或者你的代码逻辑我不确定。

您可以做的事情

  1. 将调试点放在服务方法中代码的第一行,如果您无法在那里获得调试控制,那么您可能应该再次查看您的JSON,它不是应该的方式
  2. 如果您在调试中获得控制权,则逐行跟踪它。我很确定在某行会出现一些异常。如果是这样,请尝试找出原因。

这些只是我认为可能对您有所帮助的疯狂猜测!

由于这个 post 解决了问题:Remove "type" from JSON output jersey moxy 讨论了类似的问题。

我应用了 Dennis Mitchell 的解决方案,它用 @XmlType(name="") 注释我的 class Objet 这是必要的,因为 Objet 是一个子 class。但是,作为丹尼斯,我不确定为什么会这样。

谢谢大家的帮助:)