Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @ModelAttribute忽略某些字段_Java_Spring Boot - Fatal编程技术网

Java @ModelAttribute忽略某些字段

Java @ModelAttribute忽略某些字段,java,spring-boot,Java,Spring Boot,我有一个使用lombok符号的用户类 @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Builder class User { String name, String age, String city } 和具有以下函数类型的post端点 search(@ModelAttribute User user) 我只想从查询参数中获取姓名和年龄字段,而忽略城市参数。如何才能做到这一点?我将创建另一个对象,例如

我有一个使用lombok符号的用户类

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
class User { 
    String name,
    String age,
    String city
}
和具有以下函数类型的post端点

search(@ModelAttribute User user)

我只想从查询参数中获取姓名和年龄字段,而忽略城市参数。如何才能做到这一点?

我将创建另一个对象,例如
InUser
,它只包含必要的属性:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
class InUser { 
    private String name;
    private String age;
}
然后打电话:

search(@ModelAttribute InUser user)

在控制器中,您可以调用映射器在
InUser

中创建一个新的
User
对象。我将创建另一个对象,例如
InUser
,它只包含必要的属性:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
class InUser { 
    private String name;
    private String age;
}
然后打电话:

search(@ModelAttribute InUser user)
在您的控制器中,您可以调用映射器在
InUser
中创建新的
User
对象