rest - org.springframework.http.MediaType 中的无效 token 字符 '/'

标签 rest spring-boot resttemplate restful-url media-type

我有一个基本的 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/

相关文章:

java - 创建和导入自定义 Spring 库,同时分离共享依赖项

rest - RestTemplate-处理自定义错误消息

ios - iOS 中的 Google Drive REST

android - 相当于 iOS RestKit for Android

spring - 如何告诉Eureka Client注册哪个端口(Spring Cloud)

java - AWS DynamoDBMapper 保存方法不断抛出 `DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted`

java - 为什么 RestTemplate 会消耗过多的内存?

java - 想要使用 post 方法的剩余模板来使用服务

rest - 使用validator.v2根据不同字段的值验证字段

c# - 使用 RESTSHARP 时 API 返回错误