Java 我正在编写一个控制器,在SpringBootWebFlux中列出员工id和姓名。但是在列表控制器中也要获取浮点值
我使用JDBC模板从数据库中检索详细信息,在db中有一个浮点值为0.0的字段。我只希望这个get请求包含员工姓名和id,但不确定为什么我在控制器中也会得到这个浮点值 请帮我解决这个问题。谢谢大家!Java 我正在编写一个控制器,在SpringBootWebFlux中列出员工id和姓名。但是在列表控制器中也要获取浮点值,java,mysql,jdbc,controller,resultset,Java,Mysql,Jdbc,Controller,Resultset,我使用JDBC模板从数据库中检索详细信息,在db中有一个浮点值为0.0的字段。我只希望这个get请求包含员工姓名和id,但不确定为什么我在控制器中也会得到这个浮点值 请帮我解决这个问题。谢谢大家! public Flux<Emplotee> getAllEmployeeIdsAndName() { List<Employee> allEmployee = jdbcTemplate.query(Queries.GET_ALL_EMPLOYEE_IDS_N
public Flux<Emplotee> getAllEmployeeIdsAndName() {
List<Employee> allEmployee = jdbcTemplate.query(Queries.GET_ALL_EMPLOYEE_IDS_NAME, new RowMapper<Employee>() {
@Override
public Employee mapRow(ResultSet resultSet, int i) throws SQLException {
Employee employee = new Employee();
employee.setId(resultSet.getString("employee_id"));
employee.setName(resultSet.getString("employee_name"));
return employee;
}
});
return Flux.fromIterable(allEmployee);
}
我想从列表中排除此分数值
{
"score": 0.0,
"employee_name": "John doe",
"employee_id": "97e3-f566c43cac3e"
}
公营雇员{
@JsonProperty("id")
private String employee_id;
private String employee_name;
private float score;
public String getId() {
return id;
}
public void setEmployeeId(String employee_id) {
this.id = id;
}
public String getName() {
return name;
}
public void setEmployeeName(String employeeName) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
您可以在
Employee
类中使用原语float
。float的默认值为0.0f
将包装类用于float,即float
。float
的默认值为null
。因此您将得到如下响应:
{
"score": null,
"employee_name": "John doe",
"employee_id": "97e3-f566c43cac3e"
}
注意:
如果确实要忽略该属性,则可以使用@JsonIgnore
注释。@JsonIgnore
用于忽略序列化和反序列化中使用的逻辑属性。@JsonIgnore
可用于setter、getter或field
public class Employee {
@JsonProperty("id")
private String id;
private String name;
@JsonIgnore
private Float score;
public String getId() {
return id;
}
public void setEmployeeId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setEmployeeName(String name) {
this.name = name;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
}
请分享
Employee
类的源代码。请将您的问题与详细信息联系起来。不要将它们添加到注释中,这会使阅读变得非常困难。我不想在我的回答中包含分数。您可以看到我的查询只获取员工姓名和id。我如何从我的get controller中排除分数?更新的答案。您可以使用@JsonIgnore
注释。但此时,虽然也进行了反序列化,但它不会接受该属性。有关详细信息,请单击此链接。
{
"score": null,
"employee_name": "John doe",
"employee_id": "97e3-f566c43cac3e"
}
public class Employee {
@JsonProperty("id")
private String id;
private String name;
@JsonIgnore
private Float score;
public String getId() {
return id;
}
public void setEmployeeId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setEmployeeName(String name) {
this.name = name;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
}