如何使用JAXRS将BigDecimal作为JSON中的小数返回?
我将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用mapperconverter对象。
例如,您可以实现objectConverter.codec接口。
此代码对您的要求很有用。
例如:您可以在所需字段中包含
@johnzonConverter(mybigdecimalvalueconverter.class.class)
。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.