【发布时间】:2016-07-20 03:25:02
【问题描述】:
我有一个 Java REST-Service,它为客户提供了几个 CRUD 服务(使用 Grizzly 和 Jersey)。我一直在寻找这个问题好几天了,我真的不明白,因为服务器提供了另一个几乎相同的调用,它工作得很好......
这是一个不起作用的服务的代码 sn-p:
@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 Method not allowed” D:!
你知道我为什么会遇到这个问题吗?
有没有办法获得有关正在发生的错误的更多信息?用这么少的信息真的很难解决我的问题):
编辑: 我已经尝试了几件事来纠正我的问题。 首先,我试图简化我的“updateObjet”函数。我注意到一些非常奇怪的事情:
当我发送以下 json 时:
{ "code" : "codeValue" }
“UpdateObject is called”成功显示。但是,如果我发送
{ "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();
}
}
这是我的类域的代码:
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":""}]
如您所见,有一个“类型”字段,在我的类域中未指定。我在 Jersey 的文档中进行了一些搜索,但目前我无法理解这个“类型”字段的来源。 这些信息的有趣之处在于我指定的类 Objet 有一个“类型”字段。我在想也许这个神秘出现的“类型”字段干扰了我的 Objet 的“类型”字段?
【问题讨论】:
-
您的日志文件中有相关内容吗?
-
请发布堆栈跟踪
-
我的网络服务器上没有出现错误...): [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ jersey-service --- avr . 2016 年 1 月 1 日上午 9:08:28 org.glassfish.grizzly.http.server.NetworkListener 启动 INFOS:已启动侦听器绑定到 [localhost:8080] avr。 2016 年 1 月 1 日上午 9:08:28 org.glassfish.grizzly.http.server.HttpServer 启动 INFOS:[HttpServer] 已启动。 Jersey 应用程序以 WADL 开头,可在localhost:8080/webservice/application.wadl 上按回车键停止它...
-
可以同时添加 Domaine 和 Objet 类的代码吗?
标签: java web-services rest jersey