Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8”

Rob*_*eda 2 java spring spring-boot

我使用最新的 Spring Boot 版本,当前为 2.2.2-RELEASE。

我有这个端点:

@RequestMapping(method = RequestMethod.POST, value = "/test", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<?> post(@RequestParam(required = false) MultiValueMap<?, ?> paramMap) throws Exception {
    // CODE
    return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

如果我(从邮递员)调用它,将正文设置为 x-www-form.urlencoded 一切都很好,我会收到 200 OK 状态代码。

但是如果我修改上面的端点(添加另一个参数)如下:

@RequestMapping(method = RequestMethod.POST, value = "/test", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<?> post(@RequestParam(required = false) MultiValueMap<?, ?> paramMap, RequestEntity<?> req) throws Exception {
    // CODE
    return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{
    "timestamp": 1576961587242,
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
    "path": "/test"
}
Run Code Online (Sandbox Code Playgroud)

缺了点什么?

先感谢您。

Cam*_*eva 5

正如 @Sunil Dabburi 建议调试该readWithMessageConverters方法以AbstractMessageConverterMethodArgumentResolver阐明这个问题一样,一旦理解了这个问题就会变得非常简单。Spring有大约15个默认消息转换器,它们负责将传入请求的内容转换为RequestEntityHttpEntity。如果请求有主体,转换器会读取它并将其转换为它负责的对象类型。根据传入请求的媒体类型,每个转换器决定是否可以读取该请求。对于application/x-www-form-urlencoded媒体类型,默认的 Spring 转换器都无法处理此类请求。因此,就会出现上述错误。

添加自定义转换器application/x-www-form-urlencoded到 Spring 的 http 消息转换器列表中可以解决该问题。在下面的示例中,自定义转换器仅负责读取请求,并且仅负责具有所需媒体类型的请求。

** 这些读取有助于理解媒体类型本身以及 spring 如何使用它,因此自定义转换器应该如何转换传入的请求:

以下是此类自定义 http 消息转换器的示例:

@Component
public class FormUrlencodedHttpMessageConverter extends 
AbstractGenericHttpMessageConverter<MultiValueMap<String, String>> {

private static final MediaType CONVERTER_MEDIA_TYPE = MediaType.APPLICATION_FORM_URLENCODED;

public FormUrlencodedHttpMessageConverter() {
    super(CONVERTER_MEDIA_TYPE);
}

@Override
public MultiValueMap<String, String> read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    String urlEncodedData = new BufferedReader(
            new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)).readLine();

    String[] keyValuePairs = urlEncodedData.split("&");
    MultiValueMap<String, String> keyValuePairsMap = new LinkedMultiValueMap<>();

    for (String keyValuePair : keyValuePairs) {
        String[] pairElements = keyValuePair.split("=");
        String key = pairElements[0];
        String value = pairElements[1];
        keyValuePairsMap.add(key, value);
    }

    return keyValuePairsMap;
}

@Override
protected boolean canRead(@Nullable MediaType mediaType) {

    return CONVERTER_MEDIA_TYPE.includes(mediaType);
}

@Override
protected boolean canWrite(@Nullable MediaType mediaType) {

    return CONVERTER_MEDIA_TYPE.includes(mediaType);
}

@Override
protected void writeInternal(MultiValueMap<String, String> t, Type type, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    throw new RuntimeException("Method 'writeInternal' in " + this.getClass().getSimpleName() + " is not implemented");
}

@Override
protected MultiValueMap<String, String> readInternal(Class<? extends MultiValueMap<String, String>> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    throw new RuntimeException("Method 'readInternal' in " + this.getClass().getSimpleName() + " is not implemented");
}
Run Code Online (Sandbox Code Playgroud)

  • 一些评论: `canWrite` 可以返回 true,但调用 `writeInternal` 总是失败。在读取方面,绝对没有与编码相关的逻辑,这肯定会失败。您假设 UTF-8 - 如今可能是安全的,但您的情况可能会有所不同。但更重要的是,您没有进行 URL 解码(“百分比解码”)任何东西。在各种国际环境中,这可能经常会失败。我建议您更多地沿着 Spring 的 FormHttpMessageConverter 的思路实现解析,它提供了所有这些逻辑。 (2认同)