【问题标题】:Spring Cloud Contract Content type not supported不支持 Spring Cloud 合同内容类型
【发布时间】:2020-06-23 21:41:07
【问题描述】:

我正在使用 Spring Cloud 合约,但出现错误

    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 cloud contract(在我以前的工作中,没有代码了)也许是与较新版本的兼容性问题?以下是我正在使用的类:

控制器

    @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);
        }
    }

【问题讨论】:

    标签: spring-boot spring-cloud-contract contract


    【解决方案1】:

    我应该在 Github 存储库中开始搜索。这似乎是一个已知问题,这里是链接:https://github.com/spring-cloud/spring-cloud-contract/issues/1428

    这是我的工作测试基类的外观。

        @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);
            }
        }
    

    【讨论】:

      【解决方案2】:

      不配置 mockMvc 也可以解决问题,只需在 Content-Type 标头中添加 UTF-8,例如

      headers {
              contentType('application/json;charset=UTF-8')
          }
      

      【讨论】:

        猜你喜欢
        • 2013-10-27
        • 2018-10-27
        • 2017-10-21
        • 2018-12-22
        • 2019-09-02
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        • 2023-02-15
        相关资源
        最近更新 更多