500 内部服务器错误 - Jersey(Java Web 服务)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36337391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
500 Internal server error - Jersey (Java web service)
提问by M. Guillemette
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....
我有一个 Java REST-Service,它为客户端提供了几个 CRUD 服务(使用 Grizzly 和 Jersey)。我一直在寻找这个问题好几天了,但我真的不明白,因为服务器提供了另一个几乎相同的调用,它工作得很好......
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
应该使用以下 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
此服务在使用以下 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:!
遗憾的是,我收到“500 内部服务器错误”……当然,“调用此函数”从未显示……):我尝试删除整个“updateObject”函数,当我这样做时,错误变成“405 方法不允许” 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:
编辑:我已经尝试了几件事来纠正我的问题。首先,我试图简化我的“updateObjet”函数。我注意到一些很奇怪的事情:
When I send the following json:
当我发送以下 json 时:
{ "code" : "codeValue" }
"UpdateObject is called" is successfully displayed. However, if I send
成功显示“UpdateObject is called”。但是,如果我发送
{ "code" : "codeValue", "type" : "platform.field" }
Nothing is displayed.
什么都不显示。
Here is the code of my class Objet:
这是我的类 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:
这是我的类 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:
编辑 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":""}]
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?
如您所见,有一个“类型”字段,在我的类 Domaine 中未指定。我在 Jersey 的文档中搜索了一些,但目前我无法理解这个“类型”字段的来源。这个信息的有趣之处在于我指定的类 Objet 有一个“类型”字段。我在想这个神秘出现的“类型”字段可能会干扰我的 Objet 的“类型”字段?
采纳答案by M. Guillemette
Problem solved thanks to this post: Remove "type" from JSON output jersey moxywhere a similar problem is discussed.
由于这篇文章解决了问题:从 JSON 输出 jersey moxy中删除“类型”,其中讨论了类似的问题。
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.
我应用了 Dennis Mitchell 的解决方案,它用@XmlType(name="")
This is required 注释我的类 Objet,因为 Objet 是一个子类。但是,作为丹尼斯,我不确定为什么会这样。
Thank you all for your help :)
谢谢大家的帮助 :)
回答by Muhammad Suleman
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.
500 Internal server error
只是意味着在服务器中抛出了一些异常并且请求没有按预期完成。所以我猜它可能是一些null
指针异常,原因可能是你的无效JSON
或你的代码逻辑我不确定。
Things you can do
你可以做的事情
- 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 - 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.
- 将调试点放在服务方法中代码的第一行,如果您无法在那里获得调试控制,那么您可能应该
JSON
再次查看它的方式,它不应该是 - 如果您在调试中获得控制,则逐行跟踪它。我很确定在某些行会引发一些异常。如果是这样,那么试着找出原因。
These are just wild guesses which i think that might help you!
这些只是疯狂的猜测,我认为可能会对您有所帮助!