org.springframework.http.MediaType 中的无效标记字符“/”

Invalid token character '/' in org.springframework.http.MediaType

我有一个基本的 SpringBoot 2.1.5.RELEASE 应用程序。使用 Spring 初始化程序,JPA,嵌入式 Tomcat;

我想创建这个 MediaType

MediaType mediaType = new MediaType("application/vnd.bonanza+xml");

在 PostMan 中工作正常,但在 RestTemplate 中不行

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.ideefecloud.IdeefeCloudApplication.main(IdeefeCloudApplication.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: Invalid token character '/' in token "application/vnd.bonanza+xml"

媒体类型由类型子类型组成。创建MediaType, you could either split type and subtype in the constructor的实例,如下图:

MediaType mediaType = new MediaType("application", "vnd.bonanza+xml");

或者您可以改用 valueOf() 工厂方法:

MediaType mediaType = MediaType.valueOf("application/vnd.bonanza+xml");

您可能用错了constructor。此构造函数仅将 type 作为 arguments 并将 treats 子类型作为 *

变化:

MediaType mediaType = new MediaType("application/vnd.bonanza+xml");

收件人:

MediaType mediaType = MediaType.valueOf("application/vnd.bonanza+xml");

valueOf

public static MediaType valueOf(String value)

Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.

Parameters:

value - the string to parse

Throws:

InvalidMediaTypeException - if the media type value cannot be parsed

或:

MediaType mediaType = new MediaType("application", "vnd.bonanza+xml");

MediaType(String type, String subtype)

Create a new MediaType for the given primary type and subtype.

或:

MediaType mediaType = MediaType.yourType;