如何使用JAXRS将BigDecimal作为JSON中的小数返回?

发布于 2025-01-26 20:22:53 字数 1230 浏览 3 评论 0原文

我将Tomee 8用作应用程序服务器,当我的REST服务返回BigDecimal时,我遇到了麻烦。

这是我的服务:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/v0")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RSDummyCheck {

    @Path("/dummyCheck")
    @POST
    public Response dummyCheck(Dummy input){
        Dummy resultado = input;
        return Response.ok(resultado, "application/json").build();
    }
 }

输入是

import java.math.BigDecimal;
import java.util.Date;

public class Dummy{
    BigDecimal numero;

    public BigDecimal getNumero() {
        return numero;
    }
    public void setNumero(BigDecimal numero) {
        this.numero = numero;
    }
    
    public String toString() {
        return "Dummy numero:"+this.numero

    }
}

如此,当我尝试服务时,发送此JSON消息时:

{
    
    "numero": 23.4
    
}

我得到了此响应

{
    "numero": "23.4"
}

,但我希望没有引号而不是字符串接收。

Tomee 8默认使用 Apache Johnzon 作为JSON提供商。有问题吗?

这里怎么了?为什么返回值显示为字符串而不是小数点?

I'm using Tomee 8 as an application server and I have this trouble when my rest service returns a BigDecimal.

This is my service:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/v0")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RSDummyCheck {

    @Path("/dummyCheck")
    @POST
    public Response dummyCheck(Dummy input){
        Dummy resultado = input;
        return Response.ok(resultado, "application/json").build();
    }
 }

The input is

import java.math.BigDecimal;
import java.util.Date;

public class Dummy{
    BigDecimal numero;

    public BigDecimal getNumero() {
        return numero;
    }
    public void setNumero(BigDecimal numero) {
        this.numero = numero;
    }
    
    public String toString() {
        return "Dummy numero:"+this.numero

    }
}

So, when I try the service, sending this json message:

{
    
    "numero": 23.4
    
}

I got this response

{
    "numero": "23.4"
}

But I expect to receive the same without the quotes, not a String.

Tomee 8 by default uses Apache Johnzon as JSON provider. Is there the problem?

What is wrong here? Why does the return value appear as a string and not as a decimal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2025-02-02 20:22:53

您需要使用mapperconverter对象。

例如,您可以实现objectConverter.codec接口。

此代码对您的要求很有用。

例如:您可以在所需字段中包含@johnzonConverter(mybigdecimalvalueconverter.class.class)

public class MyBigDecimalValueConverter implements ObjectConverter.Codec<BigDecimal> {

    @Override
    public void writeJson(BigDecimal value, MappingGenerator jsonbGenerator) {
        jsonbGenerator.getJsonGenerator().write(value);
    }

    @Override
    public BigDecimal fromJson(JsonValue jsonValue, Type targetType, MappingParser parser) {
        return parser.readObject(jsonValue, targetType);
    }
}

You need to use a MapperConverter object.

For example, you could implement the ObjectConverter.Codec interface.

This code can be useful for your requirement.

For example: You can include @JohnzonConverter(MyBigDecimalValueConverter.class) on the desired fields.

public class MyBigDecimalValueConverter implements ObjectConverter.Codec<BigDecimal> {

    @Override
    public void writeJson(BigDecimal value, MappingGenerator jsonbGenerator) {
        jsonbGenerator.getJsonGenerator().write(value);
    }

    @Override
    public BigDecimal fromJson(JsonValue jsonValue, Type targetType, MappingParser parser) {
        return parser.readObject(jsonValue, targetType);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文