gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:07:05 26 4
gpt4 key购买 nike

我有这个模型类:

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;
}


}

服务等级:

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;
}

}

资源类:

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;
}
}

我正在尝试以 XML 格式打印 Service(MessageService) 类中的数据。我认为,错误是由于此类方法的返回值(返回类型 List<Message> )和标签 @XMLRootElement 引起的。上面的Model(Message)类并不一致。我尝试了不同的 MediaType 属性,但没有任何帮助。

当我访问此路径时:localhost:8080/messengerapp/webapi/messageresource
我收到此错误 - Internal Server Error

我刚刚开始学习编写网络服务。我尝试了不同的方法来解决这个问题,但没有任何帮助我。请帮助我理解并解决这个问题。

最佳答案

试试这个,

注释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;
}

}

xml的结构

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

<Message>
...
</Message>

<Message>
...
</Message>

</Messages>

关于java - 获取内部服务器错误 - @XmlRootElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55935516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com