JSON.toJSONString方法与toString、String.valueOf方法的区别

时间:2024-03-01 09:34:57
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;


@Data
public class ResetPwdModel implements Serializable {
    @ApiModelProperty(value = "工号")
    @NotBlank(message = "工号不能为空")
    private String jobNumber;

    @ApiModelProperty(value = "新密码")
    @NotBlank(message = "密码不能为空")
    private String pwd;

    @ApiModelProperty(value = "手机号")
    private String mobile;

    @ApiModelProperty(value = "验证码")
    @NotBlank(message = "短信验证码不能为空")
    private String smsCode;

    public static void main(String[] args) {
        ResetPwdModel model = new ResetPwdModel();
        model.setJobNumber("111111");
        model.setPwd("123456");
        model.setMobile("151554227777");
        model.setSmsCode("456895");
        System.out.println(String.valueOf(model));
        System.out.println(model.toString());
        System.out.println(JSON.toJSONString(model));
        System.out.println(JSONObject.toJSONString(model));
    }
}

结果

ResetPwdModel(jobNumber=111111, pwd=123456, mobile=151554227777, smsCode=456895)
ResetPwdModel(jobNumber=111111, pwd=123456, mobile=151554227777, smsCode=456895)
{"jobNumber":"111111","mobile":"151554227777","pwd":"123456","smsCode":"456895"}
{"jobNumber":"111111","mobile":"151554227777","pwd":"123456","smsCode":"456895"}