如何使用Jackson /其他库将json响应对象映射为首选格式?

Gre*_*ion 5 java spring json jackson spring-boot

我从第三方Web服务获取以下JSON响应格式:

{
    "meta": {
        "code": 200,
        "requestId": "1"
    },
    "response": {
        "locations": [
            {
                "id": "1",
                "name": "XXX",
                "contact": {
                    phone: '123',
                    email: 'abc'
                },
                "location": {
                    "address": [
                        "Finland"
                    ]
                }
            },
            {
                // another location
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我应该从自己的Web服务返回的响应:

[
    {
        "id": "1",
        "name": "XXX",
        "phone": '123',
        "address": "Finland"
    },
    {
        // another location
    }
]
Run Code Online (Sandbox Code Playgroud)

我该怎么办?我读了一些有关Jackson的好东西,但是只有几个简单的示例,您可以将一些简单的JSON obj映射到POJO。在我的情况下,我需要删除一些节点,并遍历层次结构以获取嵌套的值。到目前为止,这是我在春季启动应用程序中迈出的第一步:

    @GET
    @Path("{query}")
    @Produces("application/json")
    public String getVenues(@PathParam("query") String query){
        return client.target(url).queryParam("query",query).request(...).get(String.class)
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助,指点,建议都欢迎!

ner*_*erd 4

您正在使用 JAX-RS 注释而不是 Spring Web 服务注释。您可以完成这项工作,但我建议使用默认的 Spring 注释,因为如果您使用 spring boot starter 依赖项,这些注释都会为您自动配置。

首先 - 您需要创建像请求和响应一样设置的类。像这样的东西:

public class ThirdPartyResponse {   
    MetaData meta;
    Response response;
}

public class Response {
    List<Location> locations;
}

public class MetaData {
    String code;
    String requestId;    
}

public class Location {
    String id;
    String name;
    Contact contact;
    LocationDetails location;
}

public class Contact {
    String phone;
    String email;
}

public class LocationDetails {
    List<String> address;
}
Run Code Online (Sandbox Code Playgroud)

您可以使用 Jackson 注释来自定义反序列化,但默认情况下,它按名称和您可能期望的类型非常逻辑地映射到字段(名为“locations”的 JSON 列表映射到名为“locations”的对象中的列表,等等) 。

接下来,您需要@RestController为您的服务使用带注释的类,这使得服务调用第三方服务RestTemplate,如下所示:

@RestController
public class Controller {

    @Value("${url}")
    String url;

    @RequestMapping("/path"
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Location> locations(@RequestParam String query) {

        // RestTemplate will make the service call and handle the 
        // mapping from JSON to Java object

        RestTemplate restTemplate = new RestTemplate();
        ThirdPartyResponse response = restTemplate.getForObject(url, ThirdPartyResponse.class);

        List<Location> myResponse = new List<>();            

        // ... do whatever processing you need here ...

        // this response will be serialized as JSON "automatically"
        return myResponse;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,Spring Boot 抽象了很多 JSON 处理,使其变得非常轻松。

看看 Spring 的指南,它非常有帮助:

使用 RestTemplate 使用服务 http://spring.io/guides/gs/consuming-rest/

使用 @RestController 创建 Web 服务 https://spring.io/guides/gs/rest-service/