简体   繁体   English

Spring拦截器的json格式错误响应

[英]Error response in json format for Spring interceptor

I am writing a REST based web service. 我正在编写一个基于REST的Web服务。 I need to return all the responses as JSON format. 我需要以JSON格式返回所有响应。 I have an interceptor to validate my authentication parameters. 我有一个拦截器来验证我的身份验证参数。 On authentication failure scenario, I have to return the error response in JSON format. 在身份验证失败的情况下,我必须以JSON格式返回错误响应。

Currently i am doing 目前我在做

response.setHeader("Content-Type","application/json"); response.setHeader(“ Content-Type”,“ application / json”); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "{\\"error\\":\\"Missing Authentication Parameters\\"}"); response.sendError(HttpServletResponse.SC_UNAUTHORIZED,“ {\\”错误\\“:\\”缺少身份验证参数\\“}”“);

The response body is coming as below. 响应主体如下。

JBoss Web/2.1.3.GA - Error report JBoss Web / 2.1.3.GA-错误报告

HTTP Status 401 - {"error":"Missing Authentication Parameters"} HTTP状态401-{“错误”:“缺少身份验证参数”}

type Status report 类型状态报告

message {"error":"Missing Authentication Parameters"} 消息 {“错误”:“缺少身份验证参数”}

description This request requires HTTP authentication ({"error":"Missing Authentication Parameters"}). 说明此请求需要HTTP身份验证({“错误”:“缺少身份验证参数”})。

JBoss Web/2.1.3.GA JBoss Web / 2.1.3.GA

I need just the JSON string in response. 我只需要JSON字符串作为响应。 Please help me. 请帮我。

You should probably be using spring-security for this. 您可能应该为此使用spring-security。 If you want to do it by hand, an alternative to using sendError on the response is to use spring MVC's @ExceptionHandler along with content negotiation to return JSON. 如果您想手动执行此操作,则在响应上使用sendError的替代方法是使用spring MVC的@ExceptionHandler以及内容协商来返回JSON。

First define an error class*: 首先定义一个错误类别*:

public class Error {
    public message;
    public exception;
    public Error(String message, Exception ex) {
        this.message = message;
        this.exception = ex;
    }
}

And an exception: 还有一个例外:

public class NotAuthenticatedException extends Exception {
    // ...
}

Then in your controller you throw an exception at the appropriate time, catch it with @ExceptionHandler and return a ResponseEntity containing an Error instance and the appropriate error code. 然后,在您的控制器中,在适当的时间引发异常,使用@ExceptionHandler捕获该异常,并返回包含Error实例和适当的错误代码的ResponseEntity

@Controller
public class SimpleController {
    @RequestMapping(...)
    public String aMethod() {
        // ...
        throw new NotAuthenticatedException("Missing Authentication Parameters");
    }

    @ExceptionHandler(NotAuthenticatedException.class)
    public ResponseEntity<Error> handleNotAuthenticatedException(
            NotAuthenticatedException ex, 
            HttpServletRequest request) {
        return new ResponseEntity<Error>(
            new Error(ex.getMessage(), ex), 
            HttpStatus.UNAUTHORIZED
        );
    }
}

*use getters/setters to please the java convention gods *使用getters / setters来取悦Java惯例之神

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM