简体   繁体   中英

500 Internal server error - Jersey (Java web service)

I have a Java REST-Service which provides several CRUD services to clients (using Grizzly and Jersey). I have been searching for this problem for days now and i really don't get it, because there is another call provided by the server which is nearly the same and it works just fine....

Here is a code snippet of a service that doesn't work:

@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!");

        }
}

This service is supposed to be called with the following url: http://localhost:8080/webservice/objets/newCode

Here is a service that works:

@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!");

    }
}

This service works when called with the following url: http://localhost:8080/webservice/domaines/newDomaine

Sadly I receive a "500 internal server error"... And of course "This function is called" is never displayed... ): I've tried deleting the whole "updateObject" function, and when I do so, the error becomes "405 Method not allowed" D:!

Do you have any idea why I'm having this problem?

Is there any way to have more information about the error that is occuring? It's really hard to solve my problem with this little information ):

EDIT : I've tried several things to correct my problem. First, I've tried to simplify my "updateObjet" function. I've noticed something quite wierd:

When I send the following json:

{ "code" : "codeValue" }

"UpdateObject is called" is successfully displayed. However, if I send

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

Nothing is displayed.

Here is the code of my class 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();
}

}

And here is the code of my 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();
}

}

EDIT 2 : I've continued trying to understand my error and have a few things to add that may help: First of all, I've noticed that when I do a "GET" on http://localhost:8080/webservice/domaines , I receive the following JSON:

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

As you can see, there is a "type" field, that is not specified in my class Domaine. I've searched a bit in Jersey's documentation, but for the moment I can't understand where this "type" field comes from. What's interesting with this information is that the class Objet I specified has a "type" field. I was thinking maybe this "type" field that appears mysteriously is interfering with the "type" field of my Objet?

500 Internal server error simply mean some exception has been thrown in server and request not completed as expected. So I guess its may be some null pointer exception and the reason might be your invalid JSON or your code logic I am not sure about it.

Things you can do

  1. Put debug point on very 1st line of your code in service method if you won't get debug control there then you should probably look your JSON again its not in a way as it should be
  2. If you get control in debug then trace it line by line. I am pretty sure some exception will raise at some line. If so, then try to figure out why.

These are just wild guesses which i think that might help you!

Problem solved thanks to this post: Remove "type" from JSON output jersey moxy where a similar problem is discussed.

I applied Dennis Mitchell's solution, which is annotating my class Objet with @XmlType(name="") This was necessary because Objet is a subclass. However, as Dennis, I'm not sure why this works.

Thank you all for your help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM