在 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 一样返回它。我怎样才能做到这一点?
最佳答案
我对 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));
}
}
如果这不是您想要的,请告诉我,以便我可以扩展我的答案。
关于java - 如何将 Jackson DTO 转换为 JsonSchema?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071577/