【问题标题】:How can I convert a Jackson DTO into a JsonSchema?如何将 Jackson DTO 转换为 JsonSchema?
【发布时间】:2018-04-27 23:56:59
【问题描述】:

我试图在 SDR 不适合的情况下模仿 Spring Data REST 的 API,例如登录或密码重置路由。我有这个 DTO

public class PasswordCredential implements 
AuthenticationProvider<UsernamePasswordAuthenticationToken> {

@Email
@NotNull
@NotEmpty
private final String user;

@NotNull
@NotEmpty
private final CharSequence pass;

@JsonCreator
public PasswordCredential(
    @Nullable @JsonProperty( value = "user", access = JsonProperty.Access.WRITE_ONLY ) String user,
    @Nullable @JsonProperty( value = "pass", access = JsonProperty.Access.WRITE_ONLY ) CharSequence pass
) {
    this.user = user;
    this.pass = pass;
}

我想将其转换为JsonSchema,以便我可以像 SDR 一样返回它。我怎样才能做到这一点?

【问题讨论】:

  • 你试过 gson 库了吗?
  • ...如何为 Jackson dto 正确创建 jsonschema

标签: java jackson spring-data-rest jsonschema


【解决方案1】:

我不熟悉 Spring,但我们使用 Gson 将 DTO 转换为字符串。这只是一个测试,但你明白了。

import com.google.gson.GsonBuilder;

public class NewMain {  

    static public class PasswordCredential {
        private String user;
        private CharSequence pass;
    } 

    public static void main(String[] args) {
        PasswordCredential pc = new PasswordCredential();
        pc.pass = "password";
        pc.user = "myuser";
        GsonBuilder builder = new GsonBuilder();
        System.out.println(builder.create().toJson(pc));
    }

}

如果这不是您想要的,请告诉我,以便我扩展我的答案。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
相关资源
最近更新 更多