-1

I have some BigDecimal values in database, like 10000.00, 12357.87 etc.

Now I'm trying to get these values by using GET API REST in spring boot, but here the problem is in Swagger or Postman response I see like:

"amount":"10000.00"

but I want value without quotes "amount": 10000.00

Response class:

public class ResponseDTO{
  @JsonFormat(shape = JsonFormat.Shape.STRING)
  private BigDecimal amount;
}

If I removed @JsonFormat(shape = JsonFormat.Shape.STRING) annotation it will shown as "amount":10000

Durga
  • 7
  • 4
  • 1
    So when you say 'keep zeroes after decimal point' you really mean 'remove quotation marks'? And do you realize that 10000 and 10000.00 are the same value? – user207421 Aug 16 '22 at 08:16
  • @user207421 i don't want remove the zeros, i need zeros in response – Durga Aug 16 '22 at 08:25

1 Answers1

0

You can set "scale" value of amount as 2.

Try something like this;

instead of responsedto.setAmount(amount) use responsedto.setAmount(amount.setScale(2))

With this you don't need to use @JsonFormat annotation.

Omeerfk
  • 128
  • 2
  • 11
  • tried but **.00** not showing in response **.87** or **.01** like this values coming in response – Durga Aug 16 '22 at 07:46
  • Are you sure that you need to see .00 instead of .87? Maybe you can round the number. with amount.setScale(2, RoundingMode.HALF_UP) somethingk like this. see this: https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html – Omeerfk Aug 16 '22 at 07:50
  • only .00 or .0 values not coming other values coming. – Durga Aug 16 '22 at 07:52
  • It is missing while coming to response – Durga Aug 16 '22 at 07:52
  • For your problem this answer: https://stackoverflow.com/a/65083720/5802053 is the way to go – Valentyn Aug 16 '22 at 07:59
  • @Valentyn i tried this **@JsonSerialize(using = MoneySerializer.class)** also but it will return values in string format like **"amount":"10000.00"** but i don't want quotes – Durga Aug 16 '22 at 08:02
  • @Durga As you need your number written as number (not as string) you should write your custom serialiser as in link I've posted or use https://github.com/zalando/jackson-datatype-money module – Valentyn Aug 16 '22 at 08:05
  • @Valentyn 12357.87 value is coming fine but here only problem with 10000.00, if zeros is there after decimal point then only we are not getting in response – Durga Aug 16 '22 at 08:14
  • @Valentyn custom serialiser also giving values in string format. – Durga Aug 16 '22 at 09:16
  • @Durga - if you use `writeNumber` in it then either your serializer is not invoked or something replaces its result - check with debugger that serializer is invoked. Jackson `writeNumber` writes input as number, there is no way for it to do something else – Valentyn Aug 16 '22 at 09:19
  • @Valentyn writeNumber giving like **"amount":10000** only, not giving decimals – Durga Aug 16 '22 at 09:24
  • @Durga Here is complete working example: https://github.com/valb3r/flyway_9_spring_2_7_2-issue/tree/jackson If you invoke DummyController with `curl "localhost:8080/dummies/"` you will get `[{"id":1,"amount":10.12},{"id":2,"amount":12.12},{"id":3,"amount":13.00},{"id":4,"amount":14.00},{"id":5,"amount":15.00}]` just as expected. In your case you need to check your Jackson configuration and that the **custom serialiser** is really invoked – Valentyn Aug 17 '22 at 08:15
  • @Valentyn i was implemented as same ae your logic but i'm not getting, i am checking response for my api in swagger, may be swagger will block anything? – Durga Aug 17 '22 at 13:49
  • @Durga No, swagger is just an UI wrapper above your API, it should not cause the effect (however it is implementation dependent so you can check API with i.e. `curl` just to be 100% sure). Have you checked with the debugger that `custom serializer` is really invoked? Typical cases for the behaviour you observe: - Misconfigured ObjectMapper, so that serializer is invoked - Multiple ObjectMappers used in project causing the annotation not to be found due to i.e. different API versions (I.e. accidental import from shaded jar) – Valentyn Aug 18 '22 at 06:29