Spring Boot - RestController反序列化YAML上传?

时间:2016-05-19 15:51:14

标签: java spring-boot jackson yaml

如何配置Spring Boot RestController以接受YAML上传?

以下结果是415.我可以从调试中看到,我的Spring上下文中的[application/json;charset=UTF-8, application/*+json;charset=UTF-8]个实例仅支持 <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency> 。我不能成为唯一一个尝试这样做的Spring Boot用户,我很惊讶它没有只是工作 - 大多数事情都是在Spring Boot中做的!

我在POM中获得了YAML数据格式:

@RequestMapping(method=RequestMethod.POST, value="/", consumes="application/yaml")
public String upload(@RequestBody Declaration declaration) {
    //Do stuff
}

我的RestController有一个方法:

@Test
public void triggersConvergence() throws Exception {
    ClassPathResource fixture = new ClassPathResource("declaration.yml");
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Content-Type", "application/yaml");
    requestHeaders.add("Accept", "application/json");

    URI uri = new URI("http://127.0.0.1:"+port);
    byte[] bytes = new byte[(int)fixture.contentLength()];
    fixture.getInputStream().read(bytes);
    RequestEntity<byte[]> postRequest = new RequestEntity<byte[]>(bytes, requestHeaders, HttpMethod.POST, uri);

    ResponseEntity<String> response = rest.exchange(postRequest, String.class);
    assertThat(response.getStatusCode(), is(HttpStatus.OK));
    assertThat(response.getBody(), is("Converged org my-lovely-org"));
}

我的测试:

SELECT
    ` shop_items `.*
FROM
    ` shop_items `
LEFT OUTER JOIN ` shop_item_properties ` ON ` shop_items `.` shop_id ` = ` shop_item_properties `.` shop_id `
LEFT OUTER JOIN ` property_value_ints ` ON ` shop_items `.` id ` = ` property_value_ints `.` entity_id `
AND ` shop_item_properties `.` property_id ` = property_value_ints.property_id
WHERE
    ` shop_group_id ` = '835'
AND (
        ` shop_item_properties `.` property_id ` = '565'
        AND 
        (
            ` property_value_ints `.`VALUE BETWEEN 1950 AND 2011 OR ` property_value_ints `.`VALUE` = 0
        )
)
OR (
    ` shop_item_properties `.` property_id ` = '566'
    AND 
    (
        ` property_value_ints `.`VALUE BETWEEN 2001 AND 2016 OR ` property_value_ints `.`VALUE` <= 2001

    )
)
AND ` shop_items `.` deleted ` = 0
GROUP BY
    ` shop_items `.` id `
HAVING
    COUNT (
        ` shop_item_properties `.` id `
    ) = 2

2 个答案:

答案 0 :(得分:4)

虽然Spring中没有此功能,但只需2个简单步骤即可轻松添加YAMLMapper

  1. 定义支持HttpMessageConverter的自己的Content-Type: application/x-yaml

    final class YamlJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {
        YamlJackson2HttpMessageConverter() {
            super(new YAMLMapper(), MediaType.parseMediaType("application/x-yaml"));
        }
    }
    
  2. 注册您的转换器:

    @Configuration
    public class YamlConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(new YamlJackson2HttpMessageConverter());
        }
    }
    
  3. 从POJO中享受控制器方法消费生成 application/x-yaml

答案 1 :(得分:0)

是的,你可以这样做。

Maven依赖项:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-yaml</artifactId>
  <version>2.5.4</version>
</dependency>

控制器方法:

@RequestMapping(value = "/my/endpoint", method = RequestMethod.POST, consumes = "application/x-yaml")
@ResponseBody
public ResponseEntity<MyResponse> receiveYaml(@RequestBody final String yaml) {
    //unserialize yaml
}

使用curl的请求示例:

curl -X POST --header "Content-Type: application/x-yaml" --header "Accept: */*" -d "invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments: >
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338."
相关问题