我有一个基本的 SpringBoot 2.1.5.RELEASE 应用程序。使用Spring Initializer、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"
最佳答案
你可能用错了constructor .此构造函数仅接受类型作为参数并将子类型视为 *
改变:
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;
关于rest - org.springframework.http.MediaType 中的无效 token 字符 '/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446097/