gpt4 book ai didi

java - 如何将 fasterxml.jackson.ObjectMapper @Inject 到 JAX-RS 资源中?

转载 作者:行者123 更新时间:2023-11-29 08:46:33 25 4
gpt4 key购买 nike

我正在尝试将 ObjectMapper 类的一个实例注入(inject)到我的 JAX-RS 资源中,但我看到了这个错误,

[ERROR   ] Api type [com.fasterxml.jackson.databind.ObjectMapper] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name : mapper, Bean Owner : [PageMetaResource, Name:null, WebBeans Type:MANAGED, API Types:[com.lmig.pi.ado.meta.resources.PageMetaResource], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]

我在 WEB-INF 目录中有一个空的 beans.xml 文件,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

我想知道是不是我的 Application.getSingletons() 方法导致了这个问题,

@Override
public Set<Object> getSingletons()
{
Set<Object> set = new HashSet<>();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

ObjectMapper mapper = new ObjectMapper();

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setDateFormat(df);

JacksonJsonProvider provider = new JacksonJsonProvider();
provider.setMapper(mapper);

set.add(provider);

return set;
}

我的资源类是这样的,

public class PageMetaResource extends BasePresentationResource
{
/**
* The logger of the class
*/
private static final Logger logger = Logger.getLogger(PageMetaResource.class);

/**
* Factory that produces a {@link PageMetaServices} instance for this resource.
*/
private ServiceFactory serviceFactory;

/**
* Factory that produces a {@link PageMetaResourceBean} instance for this resource.
*/
private ResourceBeanFactory resourceBeanFactory;

/**
* The servlet context.
*/
@Context
private ServletContext context;

@Inject
private ObjectMapper mapper;

/**
* Constructor
*/
@Inject
public PageMetaResource(ServiceFactory serviceFactory, ResourceBeanFactory resourceBeanFactory)
{
this.serviceFactory = serviceFactory;
this.resourceBeanFactory = resourceBeanFactory;
}
...
}

有什么想法吗?我尝试将 @inject 注入(inject)到构造函数中 - 但抛出了类似的错误。

感谢您的帮助,

安德鲁

PS:我正在部署到 Websphere Application Server v8.5 Liberty Profile

最佳答案

您的 CDI 容器未找到 ObjectMapper 的生产者。看看here .

关于java - 如何将 fasterxml.jackson.ObjectMapper @Inject 到 JAX-RS 资源中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038908/

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