gpt4 book ai didi

java - jaxrs 找不到 joda.money 类型的自定义(反)序列化器

转载 作者:行者123 更新时间:2023-11-30 02:56:46 52 4
gpt4 key购买 nike

我已经为 joda.money.Money 类型编写了自定义(反)序列化器。我用对象映射器注册它们。但是当我部署 war 文件时,它说找不到 joda.money.Money 类型的序列化器。

import org.joda.money.Money;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class MoneyDeserializer extends StdDeserializer<Money> {

private static final long serialVersionUID = 1L;

public MoneyDeserializer() {
super(Money.class);
}

@Override
public Money deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
...
}
}

在ObjectMapper中注册;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.joda.money.Money;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import xx.serializers.MoneyDeserializer;
import xx.serializers.MoneySerializer
@Provider
public class JsonProvider extends JacksonJsonProvider {

public JsonProvider() {

ObjectMapper mapper = new ObjectMapper();

mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

// Register <Money> (de)serializers
SimpleModule tstmodule = new SimpleModule("MyModule", new Version(1, 0, 0, null));
tstmodule.addDeserializer(Money.class, new MoneyDeserializer());
tstmodule.addSerializer(Money.class, new MoneySerializer());
mapper.registerModule(tstmodule);

mapper.registerModule(new JodaModule());

}

}





2:24:00,860 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 151) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./api: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./api: java.lang.RuntimeException: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.QueryParam("sale") on public javax.ws.rs.core.Response com.xx.getItems(java.lang.Integer,java.lang.Integer,java.lang.String,java.lang.Long,org.joda.money.Money,java.lang.String,java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime,java.time.LocalDateTime,java.lang.Long,java.lang.Long,java.lang.Long,java.lang.Long,java.lang.String) for basetype: org.joda.money.Money
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.RuntimeException: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.QueryParam("sale") on public javax.ws.rs.core.Response xx.getItems(java.lang.Integer,java.lang.Integer,java.lang.String,java.lang.Long,org.joda.money.Money,java.lang.String,java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime,java.time.LocalDateTime,java.lang.Long,java.lang.Long,java.lang.Long,java.lang.Long,java.lang.String) for basetype: org.joda.money.Money
at org.jboss.resteasy.core.StringParameterInjector.initialize(StringParameterInjector.java:218)

最佳答案

我看到您在 JsonProvider 构造函数中创建了 ObjectMapper 但从未使用它。您可能应该在构造函数的最后对 JsonProvider 使用 setMapper(mapper);

但我不认为这能解决你的问题。我认为问题在于 jaxrs 仅理解简单的数据类型,如果您想使用自定义类,则必须为基于字符串的 @*Param

实现某种字符串编码

从你的堆栈跟踪中我看到你使用jboss,所以也许这可以帮助? https://docs.jboss.org/resteasy/docs/3.0.12.Final/userguide/html/StringConverter.html

What if you have a class where valueOf() or this string constructor doesn't exist or is inappropriate for an HTTP request? JAX-RS 2.0 has the javax.ws.rs.ext.ParamConverterProvider to help in this situation. See javadoc for more details.

https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ParamConverterProvider.html

像这样的东西可能应该有效:

@Provider
public class MoneyConverterProvider implements ParamConverterProvider {

private final MoneyConverter converter = new MoneyConverter();

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (!rawType.equals(Money.class)) return null;
return (ParamConverter<T>) converter;
}

public class MoneyConverter implements ParamConverter<Money> {

public Money fromString(String value) {
if (value == null ||value.isEmpty()) return null; // change this for production
return Money.of(CurrencyUnit.EUR, Double.parseDouble(value));
}

public String toString(Money value) {
if (value == null) return "";
return value.getAmount().toString(); // change this for production
}

}
}

关于java - jaxrs 找不到 joda.money 类型的自定义(反)序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37041353/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com