![](/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.