创建一个宁静的应用程序,但不会以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 –内部服务器错误
有人可以告诉我我在这里想念的吗?
答案 0 :(得分:1)
您需要将Message
类中的所有字段设为私有。如果您将它们保留为公共属性,那么JAXB会将其视为属性,并且将其视为重复属性,因为您还具有JavaBean属性(getters / setters)。
@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
您可以清楚地看到问题所在。除了避免出现此错误外,这还是封装应该起作用的方式。这些字段应为私有字段,并通过getter和setter公开。