gpt4 book ai didi

java - 如何在 Java 中参数化响应解析?

转载 作者:行者123 更新时间:2023-12-02 09:18:29 25 4
gpt4 key购买 nike

我正在编写一个网络类,并且希望能够解析对不同类的不同响应(仍然存在一对一关系,但我希望有一个 parseResponse() 来处理来自不同端点的所有响应,并且 endpoint.className 具有我应该映射到的预期 classType):

private Class<?> parseResponse(StringBuilder responseContent, Endpoint endpoint) {
ObjectMapper mapper = new ObjectMapper();
try {
Class<?> object = mapper.readValue(responseContent.toString(), endpoint.className);
// endpoint.className has Class<?> type
if (object instanceof endpoint.className) {
}
} catch (IOException e) {
// handle errors
}
}

但是如果我写 if (object instanceof endpoint.className)

就会出错

更新:可能更好的选择是将 parse() 方法添加到 Endpoint 类:

public Class<?> parseResponse(String responseContent) {
// this.className has Class<?> type (e.g., Foo.class).
}

public enum Endpoint {
FOO (Foo.class),
BAR (Bar.class);

private Class<?> classType;
}

但仍然存在相同类型的错误。

最佳答案

您应该将 JSON 反序列化与应用的其他部分分开。您无法为所有响应实现一种方法,但您的响应数量可能有限,并且您可以为每个类声明一些简单的方法。一般来说,您只能有一种方法,其声明如下:

public <T> T deserialise(String payload, Class<T> expectedClass) {
Objects.requireNonNull(payload);
Objects.requireNonNull(expectedClass);

try {
return mapper.readValue(payload, expectedClass);
} catch (IOException e) {
throw new IllegalStateException("JSON is not valid!", e);
}
}

现在,您可以反序列化您想要的所有有效负载。您需要提供您想要接收的 JSON 负载和 POJO 类。

显示该概念的简单工作解决方案:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.Objects;

public class JsonMapper {

private final ObjectMapper mapper = new ObjectMapper();

public JsonMapper() {
// configure mapper instance if required
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// etc...
}

public String serialise(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not generate JSON!", e);
}
}

public <T> T deserialise(String payload, Class<T> expectedClass) {
Objects.requireNonNull(payload);
Objects.requireNonNull(expectedClass);

try {
return mapper.readValue(payload, expectedClass);
} catch (IOException e) {
throw new IllegalStateException("JSON is not valid!", e);
}
}

public Foo parseResponseFoo(String payload) {
return deserialise(payload, Foo.class);
}

public Bar parseResponseBar(String payload) {
return deserialise(payload, Bar.class);
}

public static void main(String[] args) {
JsonMapper jsonMapper = new JsonMapper();

String bar = "{\"bar\" : 2}";
System.out.println(jsonMapper.parseResponseBar(bar));

String foo = "{\"foo\" : 1}";
System.out.println(jsonMapper.parseResponseFoo(foo));

System.out.println("General method:");
System.out.println(jsonMapper.deserialise(foo, Foo.class));
System.out.println(jsonMapper.deserialise(bar, Bar.class));
}
}

class Foo {

public int foo;

@Override
public String toString() {
return "Foo{" +
"foo=" + foo +
'}';
}
}

class Bar {

public int bar;

@Override
public String toString() {
return "Bar{" +
"bar=" + bar +
'}';
}
}

另请参阅:

关于java - 如何在 Java 中参数化响应解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com