【问题标题】:RestTemplate Request/Response simple JSON-StringRestTemplate 请求/响应简单 JSON 字符串
【发布时间】:2018-08-07 21:37:58
【问题描述】:

是否有任何机构有 SpringBoot 示例 - RequestTemplate 用于获取/发布简单的原始 JSON 模板,如下例所示

请求 http://localhost:8080/SpringTest/check?JsonToTest={"name":"charlie","type":"小丑"}

响应 {"name":"charlie","type":"小丑","age":"35","country":"JPA"}

Yahooed/Googled ,...但无法找到简单的 Json-String 示例...

关于 卡提克

【问题讨论】:

  • 你的意思是RestTemplate

标签: java json spring-boot


【解决方案1】:

这对您的工具来说不是一个好主意。 我有一些建议如下:

1.HTTP获取方法(通过key=value在url路径中传值)
不能传递常用的Json-String。

2.RestTemplate的使用

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .queryParam("name", "charlie")
        .queryParam("type", "clown")
        .queryParam("age", "35")
        .queryParam("country", "JPA");


HttpEntity<?> entity = new HttpEntity<>(headers);

HttpEntity<String> response = restTemplate.exchange(
        builder.build().encode().toUri(), 
        HttpMethod.GET, 
        entity, 
        String.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2015-12-14
    • 2023-03-17
    • 1970-01-01
    • 2017-09-20
    • 2014-11-30
    相关资源
    最近更新 更多