简体   繁体   English

如何自动格式化列表中的所有 bigdecimal 字段<object>带 spring 启动<div id="text_translate"><p>我的 Rest Api (由 Spring 引导)需要自动转换 ZA8Z6FDE6331BD59EB66AC 列表中的 BigDecimal 字段。</p><p> 前任:</p><pre> class EmployeeDTO { private String name; private BigDecimal salary; // 12345 --&gt; need to 12,345 } @PostMapping(path = "/get-list-employee") public ResponseEntity&lt;Object&gt; searchListEmployee(@RequestBody EmployeeDTO employeeDTO) throws JSONException { return new ResponseEntity&lt;Object&gt;(employeeService.searchListEmployee(employeeDTO).getBody(), HttpStatus.OK); }</pre><p> 并且需要每个员工有薪水的返回列表已经格式化。</p><p> 我找到了一些格式化方式,比如使用 PropertyEditorRegistrar 或 CustomNumberFormat,...</p><p> 但似乎,它不支持列表 object。</p><p> 有人告诉我如何格式化 object 列表中的所有 bigdecimal 字段吗?</p></div></object>

[英]How to automatic fomat all bigdecimal fields in list <object> with spring boot

My Rest Api (by Spring boot) need automatic convert BigDecimal fields in list of object with thousand format.我的 Rest Api (由 Spring 引导)需要自动转换 ZA8Z6FDE6331BD59EB66AC 列表中的 BigDecimal 字段。

Ex:前任:

class EmployeeDTO {
     private String name;

     private BigDecimal salary; // 12345 --> need to 12,345
 }

@PostMapping(path = "/get-list-employee")
public ResponseEntity<Object> searchListEmployee(@RequestBody EmployeeDTO employeeDTO)
        throws JSONException {
    return new ResponseEntity<Object>(employeeService
            .searchListEmployee(employeeDTO).getBody(),
            HttpStatus.OK);
}

and need return List which each Employee has salary has formatted.并且需要每个员工有薪水的返回列表已经格式化。

I found some format ways, such as using PropertyEditorRegistrar or CustomNumberFormat, ...我找到了一些格式化方式,比如使用 PropertyEditorRegistrar 或 CustomNumberFormat,...

but seem, it not support for list object.但似乎,它不支持列表 object。

Anyone show me how to format all bigdecimal fields in a list of object?有人告诉我如何格式化 object 列表中的所有 bigdecimal 字段吗?

You can change the getter of the salary in EmployeeDTO to return a formatted value.您可以更改EmployeeDTOsalary的获取器以返回格式化值。 Like this:像这样:

public String getSalary() {
    return NumberFormat.getCurrencyInstance().format(salary);
}

This will return a formatted value according to JVM's current default Locale like this: $12,345.00这将根据 JVM 当前的默认Locale返回一个格式化的值,如下所示: $12,345.00

You can also change this if you provide a custom formatter.如果您提供自定义格式化程序,您也可以更改此设置。 Like this:像这样:

public String getSalary() {
    DecimalFormat df = new DecimalFormat("#,###.00");
    return df.format(salary);
}

It will return a formatted value like this: 12,345.00 .它将返回如下格式的值: 12,345.00

Here is how I have tested it out:这是我测试它的方法:

List<EmployeeDTO> employees = new ArrayList<>();
employees.add(new EmployeeDTO("Emp1", BigDecimal.valueOf(12345)));
employees.add(new EmployeeDTO("Emp2", BigDecimal.valueOf(22134)));
employees.add(new EmployeeDTO("Emp3", BigDecimal.valueOf(44233)));

System.out.println(new ObjectMapper().writeValueAsString(employees)); // printing JSON

Output: Output:

[
  {
    "name": "Emp1",
    "salary": "$12,345.00"
  },
  {
    "name": "Emp2",
    "salary": "$22,134.00"
  },
  {
    "name": "Emp3",
    "salary": "$44,233.00"
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM