Spring 不支持云合同内容类型
Spring Cloud Contract Content type not supported
我正在使用 spring 云合同,但出现错误
2020-06-23 23:27:41.940 WARN 39531 ---
[main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException:
Content type 'application/json;charset=ISO-8859-1' not supported]
我不确定我是否遗漏了什么。我尝试了几件事并多次阅读文档,但找不到问题所在。我过去也成功使用过 spring 云合同(在我的旧工作中,没有代码了)可能与新版本的兼容性有问题?这是我正在使用的类:
控制器
@RestController
@RequestMapping(value = "/names")
public class NameController {
@PostMapping
public Name addName(@RequestBody Name name) {
return name;
}
}
实体(名称)
@Data
public class Name {
private Long id;
private String name;
}
用于设置的基类
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@DirtiesContext
@AutoConfigureMessageVerifier
public class BaseClass {
@Autowired
private NameController nameController;
@BeforeEach
void setUp() {
StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
RestAssuredMockMvc.standaloneSetup(builder);
}
}
create_name.groovy
Contract.make {
request {
url '/names'
method 'POST'
headers {
contentType(applicationJson())
}
body([
id : 1,
name: "hello"
])
}
response {
status OK()
}
}
这里也是生成的测试
@SuppressWarnings("rawtypes")
public class ContractVerifierTest extends BaseClass {
@Test
public void validate_create_name() throws Exception {
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "application/json")
.body("{\"id\":1,\"name\":\"hello\"}");
// when:
ResponseOptions response = given().spec(request)
.post("/names");
// then:
assertThat(response.statusCode()).isEqualTo(200);
}
}
我应该在 Github 存储库中开始搜索。这似乎是一个已知问题,这里是 link:https://github.com/spring-cloud/spring-cloud-contract/issues/1428
这是我的工作测试基地 class 的样子。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class BaseClass {
@Autowired
private NameController nameController;
@BeforeEach
void setUp() {
EncoderConfig encoderConfig = new EncoderConfig(Charsets.UTF_8.name(), Charsets.UTF_8.name());
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().encoderConfig(encoderConfig);
StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
RestAssuredMockMvc.standaloneSetup(builder);
}
}
无需配置 mockMvc 也可以解决问题,只需将 UTF-8 添加到 Content-Type header,例如
headers {
contentType('application/json;charset=UTF-8')
}
我正在使用 spring 云合同,但出现错误
2020-06-23 23:27:41.940 WARN 39531 ---
[main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException:
Content type 'application/json;charset=ISO-8859-1' not supported]
我不确定我是否遗漏了什么。我尝试了几件事并多次阅读文档,但找不到问题所在。我过去也成功使用过 spring 云合同(在我的旧工作中,没有代码了)可能与新版本的兼容性有问题?这是我正在使用的类:
控制器
@RestController
@RequestMapping(value = "/names")
public class NameController {
@PostMapping
public Name addName(@RequestBody Name name) {
return name;
}
}
实体(名称)
@Data
public class Name {
private Long id;
private String name;
}
用于设置的基类
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@DirtiesContext
@AutoConfigureMessageVerifier
public class BaseClass {
@Autowired
private NameController nameController;
@BeforeEach
void setUp() {
StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
RestAssuredMockMvc.standaloneSetup(builder);
}
}
create_name.groovy
Contract.make {
request {
url '/names'
method 'POST'
headers {
contentType(applicationJson())
}
body([
id : 1,
name: "hello"
])
}
response {
status OK()
}
}
这里也是生成的测试
@SuppressWarnings("rawtypes")
public class ContractVerifierTest extends BaseClass {
@Test
public void validate_create_name() throws Exception {
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "application/json")
.body("{\"id\":1,\"name\":\"hello\"}");
// when:
ResponseOptions response = given().spec(request)
.post("/names");
// then:
assertThat(response.statusCode()).isEqualTo(200);
}
}
我应该在 Github 存储库中开始搜索。这似乎是一个已知问题,这里是 link:https://github.com/spring-cloud/spring-cloud-contract/issues/1428
这是我的工作测试基地 class 的样子。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class BaseClass {
@Autowired
private NameController nameController;
@BeforeEach
void setUp() {
EncoderConfig encoderConfig = new EncoderConfig(Charsets.UTF_8.name(), Charsets.UTF_8.name());
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().encoderConfig(encoderConfig);
StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
RestAssuredMockMvc.standaloneSetup(builder);
}
}
无需配置 mockMvc 也可以解决问题,只需将 UTF-8 添加到 Content-Type header,例如
headers {
contentType('application/json;charset=UTF-8')
}