猿问

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

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


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;

}

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


aluckdog
浏览 110回答 1
1回答

月关宝盒

您应该将JSON反序列化与应用程序的其他部分分开。您不能为所有响应实现一种方法,但您的响应数量可能有限,您可以为每个类声明一些简单的方法。通常,您只能使用一种声明如下的方法:public <T> T deserialise(String payload, Class<T> expectedClass) {&nbsp; &nbsp; Objects.requireNonNull(payload);&nbsp; &nbsp; Objects.requireNonNull(expectedClass);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return mapper.readValue(payload, expectedClass);&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("JSON is not valid!", e);&nbsp; &nbsp; }}&nbsp;现在,您可以反序列化您想要的所有有效负载。您需要提供要接收的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 {&nbsp; &nbsp; private final ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; public JsonMapper() {&nbsp; &nbsp; &nbsp; &nbsp; // configure mapper instance if required&nbsp; &nbsp; &nbsp; &nbsp; mapper.enable(SerializationFeature.INDENT_OUTPUT);&nbsp; &nbsp; &nbsp; &nbsp; mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);&nbsp; &nbsp; &nbsp; &nbsp; // etc...&nbsp; &nbsp; }&nbsp; &nbsp; public String serialise(Object value) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.writeValueAsString(value);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JsonProcessingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("Could not generate JSON!", e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public <T> T deserialise(String payload, Class<T> expectedClass) {&nbsp; &nbsp; &nbsp; &nbsp; Objects.requireNonNull(payload);&nbsp; &nbsp; &nbsp; &nbsp; Objects.requireNonNull(expectedClass);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.readValue(payload, expectedClass);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("JSON is not valid!", e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public Foo parseResponseFoo(String payload) {&nbsp; &nbsp; &nbsp; &nbsp; return deserialise(payload, Foo.class);&nbsp; &nbsp; }&nbsp; &nbsp; public Bar parseResponseBar(String payload) {&nbsp; &nbsp; &nbsp; &nbsp; return deserialise(payload, Bar.class);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; JsonMapper jsonMapper = new JsonMapper();&nbsp; &nbsp; &nbsp; &nbsp; String bar = "{\"bar\" : 2}";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonMapper.parseResponseBar(bar));&nbsp; &nbsp; &nbsp; &nbsp; String foo = "{\"foo\" : 1}";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonMapper.parseResponseFoo(foo));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("General method:");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonMapper.deserialise(foo, Foo.class));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonMapper.deserialise(bar, Bar.class));&nbsp; &nbsp; }}class Foo {&nbsp; &nbsp; public int foo;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Foo{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "foo=" + foo +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}class Bar {&nbsp; &nbsp; public int bar;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Bar{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "bar=" + bar +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答