如何在java中以json格式使jetty ErrorHandler显示错误

时间:2016-07-27 17:59:14

标签: java rest jersey embedded-jetty

我通过下面的示例代码以这种方式启动了jetty服务器,我编写了自己的errorHandler类。通过一些研究,我得到了一些信息here.但似乎还不足以得到我想要的东西。我设置为由服务器调用的类的名称是HandleClient。

因此,如果出现错误404,则显示{"message":"HTTP error 404"}。程序运行正常但是响应是文本/纯文本格式。

我的问题是:如何配置类以格式化并在MIME MediaType中显示错误:application / json。

我已经度过了这个不眠之夜。将非常感谢所有的帮助。

    public class MVCPattern{

    public MVCPattern(){
    }

    public static void main(String args[]) throws JSONException, IOException{
        MVCPattern mvcPattern = new MVCPattern();


        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        Server jettyServer = new Server(9000);
        jettyServer.setHandler(context);
        context.setErrorHandler(new ErrorHandler());
        // default error handler for resources out of "context" scope
        jettyServer.addBean(new ErrorHandler());

        ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
        jerseyServlet.setInitOrder(0);

        // Tells the Jersey Servlet which REST service/class to load.
        jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
                HandleClient.class.getCanonicalName() );

        try {
            jettyServer.start();            
            jettyServer.join();

        } catch (Exception ex) {
            Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            jettyServer.destroy();
        }
    }
    /**
     * Dummy error handler that disables any error pages or jetty related messages and returns our
     * ERROR status JSON with plain HTTP status instead. All original error messages (from our code) are preserved
     * as they are not handled by this code.
     */
    static class ErrorHandler extends ErrorPageErrorHandler {
        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.getWriter()
            .append("{\"message\":\"HTTP error ")
            .append(String.valueOf(response.getStatus()))
            .append("\"}");
        }
    }

}

2 个答案:

答案 0 :(得分:2)

您应该检查Accept中的HttpServletRequest.getHeader("Accept")标题,先查看客户端是否可以接受该类型。

然后使用HttpServletResponse.setContentType("text/json")设置响应的内容类型。

或者,如果您使用Jetty 9.3.11.v20160721(或更新版本),您可以覆盖ErrorHandler.generateAcceptableResponse(Request baseRequest, HttpServletRequest request, HttpServletResponse response, int code, String message, String mimeType)方法并相应地处理它。

请参阅:https://github.com/eclipse/jetty.project/blob/jetty-9.3.11.v20160721/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java#L196-L226例如使用它。

答案 1 :(得分:0)

这没什么大不了的。

默认情况下,Jetty使用ErrorPageErrorHandler,可以检查Jetty的WebAppContext()构造函数,默认构造函数如下所示(jetty 9.4.19.v20190610):

public WebAppContext()
    {
        this(null,null,null,null,null,new ErrorPageErrorHandler(),SESSIONS|SECURITY);
    }

sandbox

您可以扩展ErrorPageErrorHandler并写入JSON中的响应错误。

示例:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.igorkhromov.dto.Errors;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;


public class ErrorHandler extends ErrorPageErrorHandler {

    /*
        Messages to error made based on:
        Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
        https://tools.ietf.org/html/rfc7231
    */

    private static final String ERROR_404_MESSAGE = "Target resource not found";

    private static final String ERROR_501_MESSAGE = "Server functionality to process request is not implemented";

    private static final String ERROR_502_MESSAGE = "Server cannot proxy request";

    private static final String ERROR_503_MESSAGE = "Server is currently unable to handle the request";

    private static final String ERROR_504_MESSAGE = "Server did not receive a timely response from an upstream server";

    private static final String ERROR_UNEXPECTED_MESSAGE = "Unexpected error occurs";

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    protected void generateAcceptableResponse(Request baseRequest, HttpServletRequest request, HttpServletResponse response, int code, String message, String mimeType)
            throws IOException
    {

            baseRequest.setHandled(true);
            Writer writer = getAcceptableWriter(baseRequest, request, response);
            if (null != writer) {
                response.setContentType(MimeTypes.Type.APPLICATION_JSON.asString());
                response.setStatus(code);
                handleErrorPage(request, writer, code, message);
            }

    }

    @Override
    protected Writer getAcceptableWriter(Request baseRequest, HttpServletRequest request, HttpServletResponse response)
            throws IOException
    {

            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            return response.getWriter();
    }

    @Override
    protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
            throws IOException
    {

            try {
                writer.write(MAPPER.writeValueAsString(new Errors(getMessage(code))));
            }
            catch (Exception e) {
                // Log if needed
            }
    }

    private boolean isRestRequest(HttpServletRequest request) {
        return request.getServletPath().startsWith("/api/");
    }

    private String getMessage(int code) {
        switch (code) {
            case 404 : return ERROR_404_MESSAGE;
            case 501 : return ERROR_501_MESSAGE;
            case 502 : return ERROR_502_MESSAGE;
            case 503 : return ERROR_503_MESSAGE;
            case 504 : return ERROR_504_MESSAGE;
            default  : return ERROR_UNEXPECTED_MESSAGE;
        }
    }
}

我的GitHub存储库中的代码:https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java

自定义ErrorHandler:https://github.com/xrom888/blog_jetty-display-errors-in-json

获得MIT许可的所有代码,请享受))。