在Jackson中序列化路径时,移除和添加正斜杠。

huangapple go评论27阅读模式
英文:

Remove and adding forward slash when serializing Path in Jackson

问题

I am working with two APIs. One has all its paths with leading forward slashes, and the other does not. I have decided to normalize my application and use a leading forward slash for all paths (since none of the paths are relative).

If I submit JSON to API with non leading slash, with a value used for a path that has leading slash on the path, it will produce an error.

Is there any way to modify the following class so that, when it is deserialized, the Path has a leading slash, and when serialized it has no leading slash, just for this specific POJO?

public class FilePOJO {
    @JsonProperty("path")
    public Path path;
}

e.g.

FilePOJO fp = new FilePOJO();
fp.path = Paths.get("/some/path/file.txt");
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(fp));
// {"path": "some/path/file.txt"}
final FilePOJO filePOJO = om.readValue("{\"path\": \"some/path/file.txt\"}", FilePOJO.class);
System.out.println(filePOJO.path.toString());
// /some/path/file.txt
英文:

I am working with two APIs. One has all its paths with leading forward slashes, and the other does not. I have decided to normalize my application and use a leading forward slash for all paths (since none of the paths are relative).

If I submit JSON to API with non leading slash, with a value used for a path that has leading slash on the path, it will produce an error.

Is there any way to modify the following class so that, when it is deserialized, the Path has a leading slash, and when serialized it has no leading slash, just for this specific POJO?

public class FilePOJO {
    @JsonProperty("path")
    public Path path;
}

e.g.

FilePOJO fp = new FilePOJO();
fp.path = Paths.get("/some/path/file.txt");
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(fp));
// {"path": "some/path/file.txt"}
final FilePOJO filePOJO = om.readValue("{\"path\": \"some/path/file.txt\"}", FilePOJO.class);
System.out.println(filePOJO.path.toString());
// /some/path/file.txt

答案1

得分: 1

尝试在你的POJO中添加用于序列化和反序列化的getter和setter。以下是一种方法。

@JsonProperty("path")
public Path getPath() {
    return Paths.get(path.toString().substring(1));
}

@JsonProperty("path")
public void setPath(Path path) {
    this.path = Paths.get("/", path.toString());
}
英文:

Try adding getters and setters to your POJO which are used while serialization and deserialization. Below is one way of doing it.

@JsonProperty("path")
public Path getPath() {
    return Paths.get(path.toString().substring(1));
}

@JsonProperty("path")
public void setPath(Path path) {
    this.path = Paths.get("/", path.toString());
}

huangapple
  • 本文由 发表于 2020年8月1日 02:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63197534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定