You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spring 4.1 added support for protobuf. However, if an error occurs when the Accept header is set to "application/x-protobuf", then the response that is sent is a HTTP 406 Not Acceptable. For example, if a user requests a resource that requires authentication and no credentials are provided, then instead of an HTTP 401, the following response is sent:
This happens because BasicErrorController is creating an ResponseEntity<Map<String, Object>>. When ProtobufHttpMessageConverter attempts to write the body, it cannot because ProtobufHttpMessageConverter can only write protobuf Message objects (see ProtobufHttpMessageConverter supports method).
Spring Boot should provide a mechanism to ensure that the proper error is delivered to the client. A workaround for users is to create something like this:
Generate the respective Java classes. Then create a controller:
/* * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packagedemo;
importjava.util.Map;
importjavax.servlet.http.HttpServletRequest;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.autoconfigure.web.ErrorAttributes;
importorg.springframework.http.HttpStatus;
importorg.springframework.http.ResponseEntity;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.context.request.RequestAttributes;
importorg.springframework.web.context.request.ServletRequestAttributes;
importdemo.Data.Error.Builder;
importdemo.Data.MapFieldEntry;
/** * @author Rob Winch */@ControllerpublicclassErrorController {
privatefinalErrorAttributeserrorAttributes;
@AutowiredpublicErrorController(ErrorAttributeserrorAttributes) {
this.errorAttributes = errorAttributes;
}
@RequestMapping(value = "/error", produces = "application/x-protobuf")
@ResponseBodypublicResponseEntity<Data.Error> error(HttpServletRequestrequest) {
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
BuildererrorsBuilder = Data.Error.newBuilder();
for(Map.Entry<String, Object> error : body.entrySet()) {
demo.Data.MapFieldEntry.BuilderentryBuilder = MapFieldEntry
.newBuilder()
.setKey(error.getKey())
.setValue(String.valueOf(error.getValue()));
errorsBuilder.addErrors(entryBuilder.build());
}
Data.Errorerrors = errorsBuilder.build();
HttpStatusstatus = getStatus(request);
returnnewResponseEntity<Data.Error>(errors, status);
}
privatebooleangetTraceParameter(HttpServletRequestrequest) {
Stringparameter = request.getParameter("trace");
if (parameter == null) {
returnfalse;
}
return !"false".equals(parameter.toLowerCase());
}
privateMap<String, Object> getErrorAttributes(HttpServletRequestrequest,
booleanincludeStackTrace) {
RequestAttributesrequestAttributes = newServletRequestAttributes(request);
returnthis.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
privateHttpStatusgetStatus(HttpServletRequestrequest) {
IntegerstatusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
returnHttpStatus.valueOf(statusCode);
}
catch (Exceptionex) {
}
}
returnHttpStatus.INTERNAL_SERVER_ERROR;
}
}
Spring 4.1 added support for protobuf. However, if an error occurs when the Accept header is set to "application/x-protobuf", then the response that is sent is a HTTP 406 Not Acceptable. For example, if a user requests a resource that requires authentication and no credentials are provided, then instead of an HTTP 401, the following response is sent:
instead the error should look something like:
This happens because
BasicErrorController
is creating anResponseEntity<Map<String, Object>>
. WhenProtobufHttpMessageConverter
attempts to write the body, it cannot becauseProtobufHttpMessageConverter
can only write protobufMessage
objects (seeProtobufHttpMessageConverter
supports method).Spring Boot should provide a mechanism to ensure that the proper error is delivered to the client. A workaround for users is to create something like this:
Generate the respective Java classes. Then create a controller:
Related Links:
The text was updated successfully, but these errors were encountered: