REST Jersey导致HTTP状态500 - 内部服务器错误

时间:2014-04-15 05:56:16

标签: java html http rest jersey

考虑泽西岛代码:

package com.rest;

import javax.ws.rs.FormParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.POST;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author X3
 *
 */
@Path("/returnjson")
public class JsonReturn {

        @POST
        @Path("/inner")        
        @Produces("application/json")
        public MyJaxbBean getMsg(@FormParam("param1") String para1,
                                 @FormParam("param2") String para2) 
        {
            return new MyJaxbBean(para1 , para2 , 1);
        }


        // use this like : 
        // http://localhost:8080/REST3/test.jsp
        @POST
        @Path("/somewhere")
        @Produces("application/json")
        public JSONObject getJson(@FormParam("para1") String para1 ,
                                  @FormParam("para2") String para2) throws JSONException
        {
            JSONObject jason = new JSONObject();

            // Put a simple element
            jason.put( "aircraft", "A320");

            // Add a JSON Object
            JSONObject pilot = new JSONObject();
            pilot.put( "firstName", "John");
            pilot.put( "lastName", "Adams");
            jason.put( "pilot", pilot);

            // Accumulate values in an array
            jason.accumulate("passenger", "George Washington");
            jason.accumulate("passenger", "Thomas Jefferson");

            return jason;
        }
}

客户方:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <form action="rest/returnjson/somewhere" method="post">
                    <p>
                            The 1st parameter is : <input type="text" name="para1" />
                    </p>
                    <p>
                            The 2nd parameter is : <input type="text" name="para2" />
                    </p>
                    <input type="submit" value="Jersey me !" />
    </form>
</body>
</html>

当我提交表格时,我明白了:

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.

Apache Tomcat/7.0.50

我可以看到它到达了路径,但我似乎无法找到问题的根源。

有什么想法吗?

编辑:

Apr 15, 2014 9:30:16 AM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class org.json.JSONObject, and Java type class org.json.JSONObject, and MIME media type application/json was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class org.json.JSONObject, and Java type class org.json.JSONObject, and MIME media type application/json was not found
    ... 27 more

非常感谢

1 个答案:

答案 0 :(得分:2)

您需要将jersey-json.jar添加到类路径中。