我有一个Java REST服务,它为客户端提供了几个CRUD服务(使用Grizzly和Jersey)。我已经搜索这个问题好几天了,但我真的不明白,因为服务器提供了另一个几乎相同的调用,它工作得很好。。。。
以下是一个不起作用的服务的代码片段:
@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已调用"。但是,如果我发送
{ "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/domaines,我收到以下JSON:
[{"type":"domaine","nom":"DomaineTest0","adresse":"domaine de test 0","description":"","access":"test"},
{"type":"domaine","nom":"DomaineTest1","adresse":"Domaine de test 1","description":""}]
正如您所看到的,有一个"type"字段,在我的类Domaine中没有指定。我在Jersey的文档中搜索了一些,但目前我不明白这个"类型"字段是从哪里来的。这些信息的有趣之处在于,我指定的类Objet有一个"type"字段。我在想,也许这个神秘出现的"类型"字段正在干扰我的Objet的"类型"字段?
500 Internal server error
只是意味着服务器中抛出了一些异常,请求没有按预期完成。所以我想这可能是null
指针异常,原因可能是你的JSON
无效或你的代码逻辑,我对此不确定
可以做的事情
- 将调试点放在服务方法中代码的第一行,如果您无法在那里获得调试控制权,那么您可能应该再次查看
JSON
,它的方式与应有的不同 - 如果您在调试中获得了控制权,则逐行跟踪它。我很肯定会有一些例外出现。如果是,那就试着找出原因
这些只是胡乱猜测,我想这可能会对你有所帮助!
由于这篇文章解决了问题:删除"类型";来自JSON输出jersey moxy,其中讨论了类似的问题。
我应用了Dennis Mitchell的解决方案,即用@XmlType(name="")
注释我的类Objet。这是必要的,因为Objet是一个子类。然而,作为丹尼斯,我不知道为什么这是有效的。
感谢大家的帮助:)