gpt4 book ai didi

java - 获取自 EJB 以来的 RESTful 列表

转载 作者:行者123 更新时间:2023-12-01 11:18:56 26 4
gpt4 key购买 nike

我有一个项目 maven - EAR,同时我有一个与 jersey 一起使用的 RESTful JAX-RS,它使用 JSON,当我尝试从 EJB 获取 RESTful 列表时,什么也没有带来。但是,当在同一个 REST 中添加列表时,效果很好。!

这是同一类中的 RESTful 列表!

@Path("/courseMockRS")
@Consumes({"application/json"})
@Produces({"application/json"})
public class CourseMock_RS {

@GET
@Path("/all_courses")
public List<CourseMock> AllCourses() {
List<CourseMock> courses = getCoursesMock();
return courses;
}

public List<CourseMock> getCoursesMock() {
List<CourseMock> coursesMocks = new ArrayList<CourseMock>();

CourseMock mock = new CourseMock();
mock.setName("SOA Analist");
mock.setAuthor("SOA Education");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

mock = new CourseMock();
mock.setName("SOA Architect");
mock.setAuthor("SOA ET");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

mock = new CourseMock();
mock.setName("SOA Consultant");
mock.setAuthor("SOA ET");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

return coursesMocks;
}
}

因此效果很好!

[{"name":"SOA Analist","author":"SOA Education","releaseDate":"16/07/2015"},    
{"name":"SOA Architect","author":"SOA ET","releaseDate":"16/07/2015"},
{"name":"SOA Consultant","author":"SOA ET","releaseDate":"16/07/2015"}]

但是当实现 EJB 找不到时!,但是应用程序已部署并且无状态资源被接受。

这是来自 EJB 的资源。

DTO

public class CourseMock {

private String name;
private String author;
private String releaseDate;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getReleaseDate() {
return releaseDate;
}

public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}

外观

@Stateless
@LocalBean
public class CourseFacadeMock implements ICourseMockFacade{

private CourseMock mock;

@Override
public List<CourseMock> getCoursesMock() {
List <CourseMock> coursesMocks = new ArrayList<CourseMock>();

mock= new CourseMock();
mock.setName("Analista SOA");
mock.setAuthor("SOA Education");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

mock= new CourseMock();
mock.setName("SOA Analist");
mock.setAuthor("SOA Education");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

mock = new CourseMock();
mock.setName("SOA Architect");
mock.setAuthor("SOA ET");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

mock = new CourseMock();
mock.setName("SOA Consultant");
mock.setAuthor("SOA ET");
mock.setReleaseDate("16/07/2015");
coursesMocks.add(mock);

return coursesMocks;

}

}

界面外观

@Local
public interface ICourseMockFacade {

public List<CourseMock> getCoursesMock();
}

安静

@Path("/courseMockRS")
@Consumes({"application/json"})
@Produces({"application/json"})
public class CourseMock_RS {

@EJB
ICourseMockFacade courseMockFacade;

@GET
@Path("/all_courses")
public List<CourseMock> AllCourses() {
List<CourseMock> courses = courseMockFacade.getCoursesMock();
return courses;
}
}

因此失败。异常是 NUllPointerException。请帮忙!.

最佳答案

我已经使用您的代码创建了一个项目,假设您已经使用以下内容设置了 JAX-RS 配置类,那么它可以完美运行:

@ApplicationPath("resources")
public class JAXRSConfiguration extends Application {

}

在这种情况下,如果 EJB 未注入(inject),则问题可能出在 beans.xml 文件中,bean-discovery-mode 属性应设置为“all”。举个例子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

希望对您有所帮助。

关于java - 获取自 EJB 以来的 RESTful 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484813/

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