gpt4 book ai didi

java - Rest Web 服务不返回 XML 响应,甚至在 Eclipse 中没有登录控制台

转载 作者:行者123 更新时间:2023-12-02 10:45:55 25 4
gpt4 key购买 nike

创建一个安静的应用程序,但它没有以 XML 形式返回响应。即使点击 URL“http://localhost:8080/message/webapi/messages ”,控制台也没有登录。

我返回一个列表并使用 @Produces(MediaType.APPLICATION_XML) 返回 XML 格式的响应。

MessageResource.java

package org.porwal.restful.message.resources;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.porwal.restful.message.model.Message;
import org.porwal.restful.message.service.MessageService;

@Path("/messages")
public class MessageResource {

MessageService ms = new MessageService();

@GET
@Produces(MediaType.APPLICATION_XML)
public List<Message> getMessage(){
return ms.getAllMessage();
}

}

Message.java

package org.porwal.restful.message.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement( name = "Message" )
public class Message {

public long id;
public String message;
public Date created;
public String author;

public Message() {

}

public Message(long id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
this.created = new Date();
}

public long getId() {
return id;
}
@XmlElement (name = "ID")
public void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
@XmlElement (name = "Message")
public void setMessage(String message) {
this.message = message;
}
public Date getCreated() {
return created;
}
@XmlElement (name = "Created")
public void setCreated(Date created) {
this.created = created;
}
public String getAuthor() {
return author;
}
@XmlElement (name = "Author")
public void setAuthor(String author) {
this.author = author;
}

}

如果我不使用 @XMLRootElement 注释并且 TEXT_PLAIN 通过 URL 很好地返回,那么这是有效的。我还尝试删除每个字段的 @XmlElement 但没有成功。当我删除 @XMLRootElement 时,可以在 Eclipse 控制台的日志中看到 MessageBodyWriter 错误,但是当包含 @XMLRootElement 时,Eclipse 控制台上没有日志,并且 URL“http://localhost:8080/message/webapi/messages ”会引发错误:

缺少 @XmlRootElement 时出错。

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List<org.porwal.restful.message.model.Message>. This exception comes only when i commented the line "//@XmlRootElement( name = "Message" )".

HTTP 状态 500 – 内部服务器错误

有人可以告诉我这里缺少什么吗?

最佳答案

您需要将 Message 类中的所有字段设为私有(private)。如果您将它们保留为公共(public),那么 JAXB 会将其视为属性,并将其视为重复属性,因为您也有 JavaBean 属性(getter/setter)。

@XmlRootElement( name = "Message" )
public class Message {

private long id;
private String message;
private Date created;
private String author;

// ...
}
<小时/>

我是如何通过使用通用的ExceptionMapper

来解决这个问题的
@Provider
public class DebugExceptionMapper implements ExceptionMapper<Exception> {

@Override
public Response toResponse(Exception exception) {
exception.printStackTrace();
return Response.serverError().entity(exception.getMessage()).build();
}
}

您可以在您的应用程序中注册它,它会捕获未映射的异常,您可以用它做任何您想做的事情。这里我们只打印堆栈跟踪。如果我们不处理它,它就会被吞没,我们永远不知道发生了什么。

使用 ExceptionMapper 运行应用程序时,这是我收到的错误消息。

Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
Class has two properties of the same name "author"
this problem is related to the following location:
at public java.lang.String com.example.Message.getAuthor()
at com.example.Message
this problem is related to the following location:
at public java.lang.String com.example.Message.author
at com.example.Message
Class has two properties of the same name "created"
this problem is related to the following location:
at public java.util.Date com.example.Message.getCreated()
at com.example.Message
this problem is related to the following location:
at public java.util.Date com.example.Message.created
at com.example.Message
Class has two properties of the same name "id"
this problem is related to the following location:
at public long com.example.Message.getId()
at com.example.Message
this problem is related to the following location:
at public long com.example.Message.id
at com.example.Message
Class has two properties of the same name "message"
this problem is related to the following location:
at public java.lang.String com.example.Message.getMessage()
at com.example.Message
this problem is related to the following location:
at public java.lang.String com.example.Message.message
at com.example.Message

你可以清楚地看到问题所在。除了避免这个错误之外,封装应该是这样工作的;这些字段应该是私有(private)的,并通过 getter 和 setter 公开。

关于java - Rest Web 服务不返回 XML 响应,甚至在 Eclipse 中没有登录控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52578350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com