Вам нужно сделать все свои поля в классе 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
Вы можете ясно видеть, в чем проблема.И кроме того, чтобы избежать этой ошибки, именно так должна работать инкапсуляция;поля должны быть закрытыми и доступны через геттеры и сеттеры.