我有一个JAX-RS资源类,它使用@Context ResourceContext为子资源类提供路径路由,为每种资源类型创建子资源实例.在此示例中,我将实例化报告子资源.
资源
@Context
ResourceContext rc;
@Path("reports")
public ReportsResource reportsResource() {
return rc.initResource(new ReportsResource());
}
Run Code Online (Sandbox Code Playgroud)
子资源需要一个ReportService类的实例(使用@Stateless注释定义),自然的解决方案是@Inject it ...
报告SubResource
@Inject
ReportsService rs;
@GET
@Path("{rptno}")
@Produces(MediaType.APPLICATION_XML)
public Report report(@PathParam("rptno") int rptNumber) throws Exception {
return rs.getReport(rptNumber);
}
Run Code Online (Sandbox Code Playgroud)
我将Glass EE7与Glassfish和WAS Liberty Profile一起使用的经验是,不会注入ReportService rs的实例,而是将rs保留为null并导致NPE.
我的假设是因为资源类正在执行"新的ReportsResource()",所以CDI无法查看ReportsResource实例,因此ReportsResource不是容器管理的.这似乎是与通过ResourceContext获取子资源时将EJB注入JAX-RS 2.0子资源的问题相同的情况
我的解决方案有所不同,我在Resource类中选择@Inject ReportService,然后在ReportsResource构造函数上传递实例.
修改资源
@Inject
ReportsSerivce rs;
@Context
ResourceContext rc;
@Path("reports")
public ReportsResource reportsResource() {
return rc.initResource(new ReportsResource(rs));
}
Run Code Online (Sandbox Code Playgroud)
修改报告子资源
public class ReportsResource {
private ReportsSerivce rs;
public ReportsResource(ReportsSerivce rs) {
this.rs = rs;
}
@Context …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用BaseX将新元素插入到xml文档中。
declare variable $part external;
insert nodes $part as first into db:open("PARTDB")/assembly[@name="ZB09010"]
Run Code Online (Sandbox Code Playgroud)
我正在使用BaseX GUI进行测试,并定义了$ part变量(通过单击$图标)。
如果我使用“本地”变量,例如
let $up := <Employee Name="Joe">
<Personal>
<SSN>666-66-1234</SSN>
</Personal>
<StaffInfo>
<Position>Doctor</Position>
<AccountableTo>Jeff</AccountableTo>
</StaffInfo>
</Employee>
return
insert node $up as last into doc('office')/Staff
Run Code Online (Sandbox Code Playgroud)
那么插入将正常工作,但是,使用外部变量,每个保留的xml字符都将转换为xml转义字符序列,例如:<变为&lt;
我已经通过使用函数xquery:eval($ part)来包装变量来使其成功工作,但是在我看来,这就像是黑客。
我应该使用xs:string以外的其他类型来阻止翻译吗?还是我需要与外部变量一起使用以防止转换的某些功能。我也尝试用CDATA包装$ part xml内容,但是xml仍然转换为转义字符。