在序列化子类时,有什么方法可以告诉Jackson忽略父类的属性?
class Parent{ private String parentProperty1; private String parentProperty2; //getter setter } @IgnoreParentProperties // I am expecting something like this class Child extends Parent{ private String childProperty1; //getter setter }
我有一个使用jsr-303验证的bean,但BIndingResult没有返回错误.每次返回成功视图
我的豆是
public class User
{
//@NotNull
private int userId;
@NotNull
@Size(min=3,max=100)
private String userName;
@NotNull
@Size(max=60)
private String userFullName;
}
Run Code Online (Sandbox Code Playgroud)
我的控制器是
@RequestMapping(value="/user")
@Controller
public class UserController{
@RequestMapping(value="/create",method=RequestMethod.GET)
public String createUserForm(Map model)
{
model.put("user",new User());
return "createUserForm";
}
@RequestMapping(value="/create",method=RequestMethod.POST)
public String createUser (@Valid @ModelAttribute("user") User user,BindingResult result,Map model)
{
if(result.hasErrors())
{
return "createRmsUserForm";
}
else
{
model.put("User",user);
return "redirect:/home";
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种将数组转换为学说实体的方法。我正在使用原则2。
我有一个像这样的实体类:
class User
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=255, unique=true, nullable=false)
*/
protected $email;
/**
* @Column(type="string", length=64, nullable=false)
*/
protected $password;
/**
* @var DateTime
* @Column(type="datetime", nullable=false)
*/
protected $dob;
//getter and setters
}
Run Code Online (Sandbox Code Playgroud)
当我从html表单发布数据时,我想将post数组转换为User实体。所以我有一个像
$userAsArray = array("email"=>"abc@xyz.com","password"=>"hello","dob"=>"10\20\1990");
$user = new User();
covert($userAsArray,$user) // I am looking for something like this
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种通用的方式来实现这一目标。我已经尝试过这样的事情:
function fromArray(array $array,$class){
$user = new $class();
foreach($array as $key => $field){
$keyUp = ucfirst($key); …
Run Code Online (Sandbox Code Playgroud)