返回对象时出现Http 500错误

时间:2014-10-07 16:47:27

标签: java rest http

我通过在其中包装另一个REST服务来开发REST服务。 我的方法如下所示,

@GET
@Produces("text/plain")
@Path("/projects/{projectId}/")
public Project getProject(@PathParam("projectId") String projectId) {
    resourceUrl = resourceUrl + "getProject?projectId=";
    Project project = null;
    String output = null;

    try {
        httpClient = new DefaultHttpClient();
        //HttpGet getRequest = new HttpGet(resourceUrl+"projectId="+projectId);
        HttpGet getRequest = new HttpGet(
                "https://appserver.dev.cloud.wso2.com/services/t/madusanka/projecttrackerdb-default-SNAPSHOT/getProject?projectId=" +
                projectId);
        //"https://appserver.dev.cloud.wso2.com/services/t/madusanka/projecttrackerdb-default-SNAPSHOT/getProject?projectId="

        getRequest.addHeader("accept", "application/xml");

        HttpResponse response = httpClient.execute(getRequest);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new RuntimeException("Failed with HTTP error code : " + statusCode);
        }

        HttpEntity httpEntity = response.getEntity();
        //output = EntityUtils.toString(httpEntity);

        JAXBContext jaxbContext = JAXBContext.newInstance(Project.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        project = (Project) unmarshaller.unmarshal(new StringReader(output));

        //Verify the populated object
        //System.out.println(project.getTeamId());
        //System.out.println(project.getProjectName());
        //System.out.println(project.getProjectManager());*/

        //output = project.getProjectName().toString();

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } finally {
        //Important: Close the connect
        httpClient.getConnectionManager().shutdown();
    }
    return project;
}

执行时会出现以下错误。但是当我返回EntityUtils.toString(httpEntity);

时,它工作正常

错误:

<html>
<head>
    <title>Apache Tomcat/7.0.34 - Error report</title>
    <style>
        <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
    </style>
</head>
<body>
    <h1>HTTP Status 500 - org.apache.cxf.interceptor.Fault</h1>
    <HR size="1" noshade="noshade">
        <p>
            <b>type</b> Exception report
        </p>
        <p>
            <b>message</b>
            <u>org.apache.cxf.interceptor.Fault</u>
        </p>
        <p>
            <b>description</b>
            <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
        </p>
        <p>
            <b>exception</b>
            <pre>java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:331)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)

            

            

                根本原因                 

org.apache.cxf.interceptor.Fault
    org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162)
    org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:194)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:100)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:57)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:93)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
    org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)
            

            

                根本原因                 

java.lang.NullPointerException
    java.io.StringReader.<init>(StringReader.java:50)
    org.wso2.projecttracker.services.ProjectServices.getProject(ProjectServices.java:65)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
    org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:194)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:100)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:57)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:93)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
    org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)
            

            

                注意的                 Apache Tomcat / 7.0.34日志中提供了根本原因的完整堆栈跟踪。             

                             

Apache Tomcat / 7.0.34

                     

我的豆子在下面:

@XmlRootElement(name = "org.wso2.projecttracker.model.Project")

public class Project {
private String projectId;
private String projectName;
private String startDate;
private String endDate;
private String projectManager;
private String status;
private String teamId;

public Project() {
}

public String getProjectName() {
    return projectName;
}
public void setProjectName(String projectName) {
    this.projectName = projectName;
}
public String getStartDate() {
    return startDate;
}
public void setStartDate(String startDate) {
    this.startDate = startDate;
}
public String getEndDate() {
    return endDate;
}
public void setEndDate(String endDate) {
    this.endDate = endDate;
}
public String getProjectManager() {
    return projectManager;
}
public void setProjectManager(String projectManager) {
    this.projectManager = projectManager;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getTeamId() {
    return teamId;
}
public void setTeamId(String teamId) {
    this.teamId = teamId;
}
public String getProjectId() {
    return projectId;
}
public void setProjectId(String projectId) {
    this.projectId = projectId;
}

}

请任何人帮助我。在此先感谢。!!!

0 个答案:

没有答案