Java 17 java.lang.reflect.无法访问对象异常:无法使字段私有化最终java.time.ZoneOffset

1cklez4t  于 2023-01-29  发布在  Java
关注(0)|答案(2)|浏览(506)

我一直在尝试将一个json字符串转换为我的java程序可以使用的变量,但是当我运行这个时它不起作用。(responseString是一个有效的json字符串)

GetConversion conversion = new ObjectMapper().readValue(responseString, GetConversion.class);

class GetConversion{
private State state;
private Swupdate swupdate;
private String type;
private String name;
private String modelid;
private String manufacturername;
private String productname;
private Capabilities capabilities;
private Config config;
private String uniqueid;
private String swversion;
private String swconfigid;
private String productid;

public State getState() { return state; }
public void setState(State value) { this.state = value; }

public Swupdate getSwupdate() { return swupdate; }
public void setSwupdate(Swupdate value) { this.swupdate = value; }

public String getType() { return type; }
public void setType(String value) { this.type = value; }

public String getName() { return name; }
public void setName(String value) { this.name = value; }

public String getModelid() { return modelid; }
public void setModelid(String value) { this.modelid = value; }

public String getManufacturername() { return manufacturername; }
public void setManufacturername(String value) { this.manufacturername = value; }

public String getProductname() { return productname; }
public void setProductname(String value) { this.productname = value; }

public Capabilities getCapabilities() { return capabilities; }
public void setCapabilities(Capabilities value) { this.capabilities = value; }

public Config getConfig() { return config; }
public void setConfig(Config value) { this.config = value; }

public String getUniqueid() { return uniqueid; }
public void setUniqueid(String value) { this.uniqueid = value; }

public String getSwversion() { return swversion; }
public void setSwversion(String value) { this.swversion = value; }

public String getSwconfigid() { return swconfigid; }
public void setSwconfigid(String value) { this.swconfigid = value; }

public String getProductid() { return productid; }
public void setProductid(String value) { this.productid = value; }
}

class Capabilities {
   private boolean certified;
   private Control control;
   private Streaming streaming;

    public boolean getCertified() { return certified; }
    public void setCertified(boolean value) { this.certified = value; }

    public Control getControl() { return control; }
    public void setControl(Control value) { this.control = value; }

    public Streaming getStreaming() { return streaming; }
    public void setStreaming(Streaming value) { this.streaming = value; }
}

class Control {
    private long mindimlevel;
    private long maxlumen;
    private String colorgamuttype;
    private double[][] colorgamut;
    private CT ct;

    public long getMindimlevel() { return mindimlevel; }
    public void setMindimlevel(long value) { this.mindimlevel = value; }

    public long getMaxlumen() { return maxlumen; }
    public void setMaxlumen(long value) { this.maxlumen = value; }

    public String getColorgamuttype() { return colorgamuttype; }
    public void setColorgamuttype(String value) { this.colorgamuttype = value; }

    public double[][] getColorgamut() { return colorgamut; }
    public void setColorgamut(double[][] value) { this.colorgamut = value; }

    public CT getCT() { return ct; }
    public void setCT(CT value) { this.ct = value; }
}

class CT {
    private long min;
    private long max;

    public long getMin() { return min; }
    public void setMin(long value) { this.min = value; }

    public long getMax() { return max; }
    public void setMax(long value) { this.max = value; }
}

class Streaming {
    private boolean renderer;
    private boolean proxy;

    public boolean getRenderer() { return renderer; }
    public void setRenderer(boolean value) { this.renderer = value; }

    public boolean getProxy() { return proxy; }
    public void setProxy(boolean value) { this.proxy = value; }
}

class Config {
    private String archetype;
    private String function;
    private String direction;
    private Startup startup;

    public String getArchetype() { return archetype; }
    public void setArchetype(String value) { this.archetype = value; }

    public String getFunction() { return function; }
    public void setFunction(String value) { this.function = value; }

    public String getDirection() { return direction; }
    public void setDirection(String value) { this.direction = value; }

    public Startup getStartup() { return startup; }
    public void setStartup(Startup value) { this.startup = value; }
}

class Startup {
    private String mode;
    private boolean configured;

    public String getMode() {
        return mode;
    }

    public void setMode(String value) {
        this.mode = value;
    }

    public boolean getConfigured() {
        return configured;
    }

    public void setConfigured(boolean value) {
        this.configured = value;
    }
}

class State {
    private boolean on;
    private long bri;
    private long hue;
    private long sat;
    private String effect;
    private double[] xy;
    private long ct;
    private String alert;
    private String colormode;
    private String mode;
    private boolean reachable;

    public boolean getOn() { return on; }
    public void setOn(boolean value) { this.on = value; }

    public long getBri() { return bri; }
    public void setBri(long value) { this.bri = value; }

    public long getHue() { return hue; }
    public void setHue(long value) { this.hue = value; }

    public long getSat() { return sat; }
    public void setSat(long value) { this.sat = value; }

    public String getEffect() { return effect; }
    public void setEffect(String value) { this.effect = value; }

    public double[] getXy() { return xy; }
    public void setXy(double[] value) { this.xy = value; }

    public long getCT() { return ct; }
    public void setCT(long value) { this.ct = value; }

    public String getAlert() { return alert; }
    public void setAlert(String value) { this.alert = value; }

    public String getColormode() { return colormode; }
    public void setColormode(String value) { this.colormode = value; }

    public String getMode() { return mode; }
    public void setMode(String value) { this.mode = value; }

    public boolean getReachable() { return reachable; }
    public void setReachable(boolean value) { this.reachable = value; }
}

class Swupdate {
    private String state;
    private OffsetDateTime lastinstall;

    public String getState() { return state; }

    public OffsetDateTime getLastinstall() { return lastinstall; }
}

我相信这可能与代码的最后一部分“public OffsetDateTime getLastinstall(){ return lastinstall;}”,因为错误提到了java.time.OffsetDateTime,而这就是调用它的地方。当我运行这个程序时,它给出了一个错误:

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.ZoneOffset java.time.OffsetDateTime.offset accessible: module java.base does not "opens java.time" to unnamed module @3578436e
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at com.fasterxml.jackson.databind.util.ClassUtil.checkAndFixAccess(ClassUtil.java:891)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.fixAccess(FieldProperty.java:102)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder._fixAccess(BeanDeserializerBuilder.java:484)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder.build(BeanDeserializerBuilder.java:350)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:245)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:137)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:411)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findNonContextualValueDeserializer(DeserializationContext.java:467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:473)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findNonContextualValueDeserializer(DeserializationContext.java:467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:473)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4190)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4009)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at sendCommands.Loft.runGet(Loft.java:141)
at sendCommands.Loft.routine(Loft.java:103)
at sendCommands.Main.main(Main.java:7)

请帮助修复此问题。我用于响应字符串的JSON是:

{
    "state": {
        "on": true,
        "bri": 198,
        "hue": 25360,
        "sat": 254,
        "effect": "none",
        "xy": [
            0.1749,
            0.6963
        ],
        "ct": 500,
        "alert": "none",
        "colormode": "xy",
        "mode": "homeautomation",
        "reachable": true
    },
    "swupdate": {
        "state": "noupdates",
        "lastinstall": "2021-08-13T13:51:54"
    },
    "type": "Extended color light",
    "name": "Hue color spot 6",
    "modelid": "LCG002",
    "manufacturername": "Signify Netherlands B.V.",
    "productname": "Hue color spot",
    "capabilities": {
        "certified": true,
        "control": {
            "mindimlevel": 200,
            "maxlumen": 300,
            "colorgamuttype": "C",
            "colorgamut": [
                [
                    0.6915,
                    0.3083
                ],
                [
                    0.17,
                    0.7
                ],
                [
                    0.1532,
                    0.0475
                ]
            ],
            "ct": {
                "min": 153,
                "max": 500
            }
        },
        "streaming": {
            "renderer": true,
            "proxy": true
        }
    },
    "config": {
        "archetype": "spotbulb",
        "function": "mixed",
        "direction": "downwards",
        "startup": {
            "mode": "safety",
            "configured": true
        }
    },
    "uniqueid": "00:17:88:01:08:3f:fb:56-0b",
    "swversion": "1.88.2",
    "swconfigid": "4AF23F6F",
    "productid": "Philips-LCG002-1-GU10ECLv2"
}
nnt7mjpx

nnt7mjpx1#

在我看来,Jackson试图序列化一个OffsetDateTime字段时出现了问题--它试图访问OffsetDateTime的私有“offset”属性。
Jackson需要知道如何正确地序列化OffsetDateTime。
您可以添加另一个Jackson库依赖项来处理这个问题,或者如果您需要该库没有提供的格式,可以编写一个自定义对象Map器。
参见Jackson date-format for OffsetDateTime in Spring Boot

2ledvvac

2ledvvac2#

我在单元测试Twilio SMS时遇到了类似的问题。
我通过在ObjectMapper上注册JavaTimeModule解决了这个问题。

String json = "{\"status\": \"sent\"}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        var message = Message.fromJson(json, mapper);

相关问题