简体   繁体   English

获取内部服务器错误 - @XmlRootElement

[英]Getting internal server error - @XmlRootElement

I have this Model class: 我有这个Model类:

package org.myapp.model;
import java.util.Date;

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


@XmlRootElement(name="Message")
@XmlAccessorType(XmlAccessType.FIELD)
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();
    }
    @XmlElement
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    @XmlElement
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    @XmlElement
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }


}

The Service Class: 服务类:

package org.myapp.services;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.myapp.model.Message;



public class MessageService {


    public List<Message> getAllMessages(){
        Message msg1 = new Message(1L,"How are you?", "natalie");
        Message msg2 = new Message(2L,"How are you?", "amir");
        List<Message> msglist = new ArrayList<Message>();
        msglist.add(msg1);
        msglist.add(msg2);
        return msglist;
    }

}

The Resource Class: 资源类:

package org.myapp.resource;

import java.util.List;

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

import org.myapp.model.Message;
import org.myapp.services.MessageService;

@Path("messageresource")
public class MessageResource {

    MessageService messageService = new MessageService();

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List<Message> getMessage() {
        return   messageService.getAllMessages();  //"app chal rhi hai!";
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("/{messageId}")
    public String getMessageID(@PathParam("messageId") String messageId) {
        Message message = new Message(1L,"How are you?", "natalie");
        return message.getMessage()+", "+messageId;
    }
}

I am trying to print the data in Service(MessageService) class in an XML format. 我试图以XML格式打印Service(MessageService)类中的数据。 I think, the error is being caused because the return value of this class' method (return type List<Message> ) and the tag @XMLRootElement on the top of the Model (Message) class are not consistent. 我认为,错误是由于此类'方法(返回类型List<Message> )的返回值和Model(Message)类顶部的标记@XMLRootElement不一致引起的。 I tried different MediaType properties but nothing helped. 我尝试了不同的MediaType属性,但没有任何帮助。

When I am accessing this path : localhost:8080/messengerapp/webapi/messageresource 当我访问此路径时: localhost:8080/messengerapp/webapi/messageresource
I am getting this error - Internal Server Error 我收到此错误 - Internal Server Error

I have just started to learn to write web services.I have tried different ways to get around this problem but nothing is helping me. 我刚开始学习编写Web服务。我尝试过不同的方法来解决这个问题,但没有什么能帮助我。 Please help me understand and solve this. 请帮助我理解并解决这个问题。

try with this, 试试这个,

annotate the root element of xml 注释xml的根元素

@XmlRootElement (name="Messages")
public class MessageService implements Serializable{

    private List<Message> msglist = new ArrayList<Message>();

    public List<Message> getAllMessages(){
        Message msg1 = new Message(1L,"How are you?", "natalie");
        Message msg2 = new Message(2L,"How are you?", "amir");
        msglist.add(msg1);
        msglist.add(msg2);
        return msglist;
    }

    public void setAllMessages(List<Message> msglist){
        this.msglist = msglist;
    }

}

structure of the xml xml的结构

<Messages>  <!--  root element of xml  -->

    <Message>
        ...
    </Message>

    <Message>
        ...
    </Message>

</Messages>

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

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