如何使用 ResponseBodyAdvice 响应不带双引号的字符串正文数据?

我使用springboot框架。我需要加密正文数据(从 json 到加密字符串)。所以我将 ResponseBodyAdvice.java 与 @ControllerAdvice 一起使用,但有一个问题,它总是用双引号响应加密数据(例如“hello”),我需要只响应 hello 而不是“hello”。


@Override

public Object beforeBodyWrite(...) {

response.getHeaders().set("content-type", "text/plain;charset=UTF-8");


//some codes..


String result = "hello";


return result;

}


它响应“你好”(我需要不带双引号的数据)


在控制器类中,它仅响应 hello(不带双引号)。请参阅下面的代码。



  @ApiOperation(value = "absdfasdf", produces = "text/plain")

  @GetMapping("/absd")

  public String asdfasdf() {

    return "hello";

  }


浮云间
浏览 245回答 1
1回答

互换的青春

您需要检查您的 MessageConverters 以确保 StringMessageConverter 在 MappingJackson2HttpMessageConverter 之前。否则,将选择 JSON MessageConverter 来序列化字符串并添加额外的双引号。@Componentpublic class MyWebMvcConfigurer implements WebMvcConfigurer {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {&nbsp; &nbsp; &nbsp; &nbsp; // check converter order here&nbsp; &nbsp; }}如以下spring源码,流程为:选择消息转换器在 ResponseBodyAdvice 中调用 beforeBodyWrite 方法转换消息AbstractMessageConverterMethodProcessor.java// choose a message converterif (genericConverter != null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; converter.canWrite(valueType, selectedMediaType)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // invoke beforeBodyWrite&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Class<? extends HttpMessageConverter<?>>) converter.getClass(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputMessage, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (body != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object theBody = body;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogFormatUtils.traceDebug(logger, traceOn ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addContentDispositionHeader(inputMessage, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (genericConverter != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; genericConverter.write(body, targetType, selectedMediaType, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert message&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }事实是我们无法更改 ResponseBodyAdvice 中的 MessageConverter。但我们可以自定义一个Dynamic MessageConverter。例如:public class DynamicMessageConverter implements HttpMessageConverter<Object> {&nbsp; &nbsp; private final HttpMessageConverter<Object> jsonConverter;&nbsp; &nbsp; private final HttpMessageConverter<String> stringConverter;&nbsp; &nbsp; public DynamicMessageConverter(HttpMessageConverter<Object> jsonConverter, HttpMessageConverter<String> stringConverter) {&nbsp; &nbsp; &nbsp; &nbsp; this.jsonConverter = jsonConverter;&nbsp; &nbsp; &nbsp; &nbsp; this.stringConverter = stringConverter;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean canWrite(Class clazz, MediaType mediaType) {&nbsp; &nbsp; &nbsp; &nbsp; return jsonConverter.canWrite(clazz, mediaType) || stringConverter.canWrite(clazz, mediaType);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<MediaType> getSupportedMediaTypes() {&nbsp; &nbsp; &nbsp; &nbsp; List<MediaType> jsonMediaTypes = jsonConverter.getSupportedMediaTypes();&nbsp; &nbsp; &nbsp; &nbsp; List<MediaType> stringMediaTypes = stringConverter.getSupportedMediaTypes();&nbsp; &nbsp; &nbsp; &nbsp; List<MediaType> all = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; all.addAll(jsonMediaTypes);&nbsp; &nbsp; &nbsp; &nbsp; all.addAll(stringMediaTypes);&nbsp; &nbsp; &nbsp; &nbsp; return all;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {&nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof String) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringConverter.write((String) o, contentType, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonConverter.write(o, contentType, outputMessage);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean canRead(Class clazz, MediaType mediaType) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object read(Class clazz, HttpInputMessage inputMessage) throws HttpMessageNotReadableException {&nbsp; &nbsp; &nbsp; &nbsp; throw new UnsupportedOperationException();&nbsp; &nbsp; }}然后启用它&nbsp; &nbsp; @Override&nbsp; &nbsp; public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {&nbsp; &nbsp; &nbsp; &nbsp; MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();&nbsp; &nbsp; &nbsp; &nbsp; StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();&nbsp; &nbsp; &nbsp; &nbsp; DynamicMessageConverter dynamicMessageConverter = new DynamicMessageConverter(jsonConverter, stringConverter);&nbsp; &nbsp; &nbsp; &nbsp; converters.add(0, dynamicMessageConverter);&nbsp; &nbsp; }直接通过response来写似乎更简洁。&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; response.getHeaders().set("content-type", "text/plain;charset=UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; //some codes..&nbsp; &nbsp; &nbsp; &nbsp; String result = "hello";&nbsp; &nbsp; &nbsp; &nbsp; try (OutputStream stream = response.getBody()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.write(result.getBytes("utf-8"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.flush();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // log ex&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java