java 注释类型的属性值未定义为“MediaType.APPLICATION_JSON”生产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30254717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:46:50  来源:igfitidea点击:

The attribute value is undefined for the annotation type Produces for "MediaType.APPLICATION_JSON"

javarestjpaannotationsjax-rs

提问by brainmassage

I am getting a weird warning (while hovering the red line in eclipse) for this very simple restful service code: (Eclipse draws a red line under "MediaType.APPLICATION_JSON")

对于这个非常简单的宁静服务代码,我收到了一个奇怪的警告(在 eclipse 中悬停红线时):(Eclipse 在“MediaType.APPLICATION_JSON”下画了一条红线)

import java.util.List;
import java.util.logging.Logger;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.myCompany.annotation.RestfulServiceAddress;
import com.myCompany.myProject.middleware.api.myService;

@RestfulServiceAddress("/myCompany-middleware")
public class myServiceImpl implements myService {

private EntityManagerFactory entityManagerFactory;

@GET
@Path("/getStuff")
@Produces(MediaType.APPLICATION_JSON)
public List<Object> getStuff() {
    EntityManager entityManager = entityManagerFactory
            .createEntityManager();
    try {
        return entityManager.createQuery(
                "Select S from Stuff S", Object.class)
                .getResultList();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    } finally {
        entityManager.close();
    }
}

public EntityManagerFactory getEntityManagerFactory() {
    return entityManagerFactory;
}

public void setEntityManagerFactory(
        EntityManagerFactory entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}
}

Also maven builds the jar file without problems. But I get an error from the OSGI container, telling me the jar is failed.

maven 也可以毫无问题地构建 jar 文件。但是我从 OSGI 容器收到一个错误,告诉我 jar 失败了。

回答by Paul Samsotha

Use @javax.ws.rs.Producesinstead of javax.enterprise.inject.Produces

使用@javax.ws.rs.Produces代替javax.enterprise.inject.Produces

import javax.ws.rs.Produces; 
// import javax.enterprise.inject.Produces;