0
votes

I have this Model class:

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. 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. I tried different MediaType properties but nothing helped.

When I am accessing this path : localhost:8080/messengerapp/webapi/messageresource
I am getting this 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. Please help me understand and solve this.

1
Fixing an error always starts by finding why it's thrown. Finding why it's thrown always starts by reading the error type, message and stack trace. Post the exact and complete stack trace of the error. - JB Nizet
You can check your hypothesis that the problem is returning a List<Message> by returning just Message, and seeing if the problem goes away. BTW, getMessage is not a good name for a function returning a list of messages. - Gonen I

1 Answers

0
votes

try with this,

annotate the root element of 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

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

    <Message>
        ...
    </Message>

    <Message>
        ...
    </Message>

</Messages>