繁体   English   中英

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

[英]Rest web services not returning XML response and even no log on console in eclipse

创建一个宁静的应用程序,但不会以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批注,并且URL很好地返回了TEXT_PLAIN,则此方法有效。 我也尝试为每个字段删除@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类中的所有字段设为私有。 如果将它们保留为公共属性,则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公开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM