如何将Jackson DTO转换为JsonSchema?

时间:2018-04-27 23:56:59

标签: java jackson spring-data-rest jsonschema

我尝试在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返回。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:-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));
    }

}

如果那不是您正在寻找的内容让我知道,那么我可以扩展我的答案。