REST请求期间的HTTP状态500

时间:2015-12-29 16:05:06

标签: java rest

我是REST网络服务的新手并且正在尝试学习它。 我有下面的代码,通过休息调用获取存储在oracle表中的消息。

import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path ("/messages")
public class messages {
@GET
@Produces(MediaType.APPLICATION_XML)
public List<Message> getMessages (){
    MessageService msg = new MessageService();
    System.out.println(msg.getMessages("vulture9"));
    return msg.getMessages("vulture9");
}

另一部分是

import java.sql.*;
import java.util.*;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MessageService {

public MessageService(){
}   
public List getMessages(String username){       
    Connection conn;
    List <Map> msgList = new LinkedList<>();        
    try {
        Class.forName ("oracle.jdbc.OracleDriver");
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@talon.squadrhino.com:1521:orcl", "oracle", "oracle");
        System.out.println("connection successful");            
        Statement stmt = conn.createStatement();
        String sql = "select * from message where author = '" + username +"'";
        ResultSet res = stmt.executeQuery(sql); 
        while (res.next()){
            Map <String,String> msg = new LinkedHashMap<>();
            msg.put("username", res.getString("author"));
            msg.put("message", res.getString("message"));
            msgList.add(msg);
        }
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return msgList;     
}
}

我正在获取存储在数据库表中的消息并尝试以XML格式获取它们。我在tomcat日志中看不到任何错误,但浏览器只返回

低于错误

HTTP Status 500 - Internal Server Error
type Status report
message Internal Server Error
description The server encountered an internal error that prevented it from fulfilling this request. 

我不确定在哪里寻找或开始调试..

1 个答案:

答案 0 :(得分:0)

尝试在Message类上添加jaxb注释。

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

或只是尝试

@XmlElement
public List getMessages(String username){ 
}

有一篇关于xml转换和java客户端的好文章:http://entityclass.in/rest/jerseyXML.htm