Rest 错误时具有不同内容类型编码的spring boot(mvc)响应

Rest 错误时具有不同内容类型编码的spring boot(mvc)响应,rest,spring-mvc,error-handling,spring-boot,Rest,Spring Mvc,Error Handling,Spring Boot,我需要spring处理错误的帮助 客户端服务正在发送一个接受两种不同内容类型(二进制和json)的请求。当一切正常时,我更喜欢用二进制编码与服务器通信以节省带宽。但在出现错误时,我希望将响应序列化为json,因为我的二进制序列化程序不知道如何将其序列化为二进制格式,而且它更适合于日志记录等 我配置了ResponseEntityExceptionHandler的实例,并且正在处理与该实现不同的异常。但是spring总是选择二进制格式,因为它是接受(或产生)列表中的第一个 我得到的只是(因为spri

我需要spring处理错误的帮助

客户端服务正在发送一个接受两种不同内容类型(二进制和json)的请求。当一切正常时,我更喜欢用二进制编码与服务器通信以节省带宽。但在出现错误时,我希望将响应序列化为json,因为我的二进制序列化程序不知道如何将其序列化为二进制格式,而且它更适合于日志记录等

我配置了ResponseEntityExceptionHandler的实例,并且正在处理与该实现不同的异常。但是spring总是选择二进制格式,因为它是接受(或产生)列表中的第一个

我得到的只是(因为spring不知道如何将响应序列化为我的自定义二进制格式。请参阅AbstractMessageConverterMethodProcessor#writeWithMessageConverters)

客户端发送

headers {Accept: [application/custom-binary, application/json]
服务器的控制器配置为

// pseudo code
@RequestMapping(method = GET, produces = {"application/custom-binary", APPLICATION_JSON_VALUE})
public BannerMetaCollection get(@RequestParam(value = "q") UUID[] q) {
    if (q != null) {
        return service.getAllDataWith(q);
    } else {
        throw new IllegalArgumentException("invalid data");
    }
}

// pseudo code
public class RestExceptionResolverSupport extends ResponseEntityExceptionHandler {
    @ExceptionHandler
    public ResponseEntity<Object> illegalArgumentException(IllegalArgumentException ex, WebRequest request {
        Object body = errorResponse()
                    .withCode(BAD_REQUEST)
                    .withDescription("Request sent is invalid")
                    .withMessage(ex.getMessage())
                    .build());
        return new ResponseEntity<Object>(body, new HttpHeaders(), HttpStatus.BAD_REQUEST);
    }
}
//伪代码
@RequestMapping(方法=GET,产生={“应用程序/自定义二进制”,应用程序\u JSON\u值})
公共BannerMetaCollection获取(@RequestParam(value=“q”)UUID[]q){
如果(q!=null){
返回服务.getAllDataWith(q);
}否则{
抛出新的IllegalArgumentException(“无效数据”);
}
}
//伪码
公共类RESTExceptionResolversSupport扩展了ResponseEntityExceptionHandler{
@例外处理程序
公共响应性illegalArgumentException(illegalArgumentException ex,WebRequest请求{
对象体=errorResponse()
.带代码(错误请求)
.withDescription(“发送的请求无效”)
.withMessage(例如getMessage())
.build());
返回新的ResponseEntity(body,new-HttpHeaders(),HttpStatus.BAD_请求);
}
}

有什么提示吗?

我要做的是让我的端点方法返回
ResponseEntity
,我不声明
@RequestMapping
注释中生成了什么内容。然后,在返回响应之前,我自己设置内容类型标题,例如

// pseudo code
@RequestMapping(method = GET)
public ResponseEntity<BannerMetaCollection> get(@RequestParam(value = "q") UUID[] q) {
    if (q != null) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "application/custom-binary");
        return new ResponseEntity<>(service.getAllDataWith(q),
                headers,
                HttpStatus.OK);
    } else {
        throw new IllegalArgumentException("invalid data");
    }
}
//伪代码
@RequestMapping(方法=GET)
公共响应获取(@RequestParam(value=“q”)UUID[]q){
如果(q!=null){
HttpHeaders=新的HttpHeaders();
headers.add(HttpHeaders.CONTENT_类型,“应用程序/自定义二进制文件”);
返回新的ResponseEntity(service.getAllDataWith(q)),
标题,
HttpStatus.OK);
}否则{
抛出新的IllegalArgumentException(“无效数据”);
}
}
// pseudo code
@RequestMapping(method = GET)
public ResponseEntity<BannerMetaCollection> get(@RequestParam(value = "q") UUID[] q) {
    if (q != null) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "application/custom-binary");
        return new ResponseEntity<>(service.getAllDataWith(q),
                headers,
                HttpStatus.OK);
    } else {
        throw new IllegalArgumentException("invalid data");
    }
}