1

我坚持使用 JAX-WS 公开 Web 服务。由于 JAXB 在休眠关闭会话后尝试序列化对象而出现问题。我在过去两天谷歌但无法找到正确的答案。以下是其他人给出的某种解决方案。

  • 急切加载对象
  • 使用@XmlTransient
  • SOAPHandler 但它没有完成答案

这是详细的场景。我写了一个模型类,它有多个@OneTOMany 关系、@ManyToOne 关系和@OneToOne 关系。结果,Web 服务返回该类的对象。当我通过 SOAP UI 调用服务时,它会给我以下错误消息。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>could not initialize proxy - no Session</faultstring>
      </S:Fault>
   </S:Body>
</S:Envelope> 

模型类(没有 getter 和 setter)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@Entity
@AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "mas_form_id")),
        @AttributeOverride(name = "tenantId", column = @Column(name = "mas_form_tenant_id")) })
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string"))
@Filters(@Filter(name = "tenantFilter", condition = "mas_form_tenant_id = :tenantIdParam"))
@Table(name = "mas_form")
public class Form extends BaseRevolution {

    @Column(name = "mas_form_action")
    private String formAction;
    @Column(name = "mas_form_is_active")
    private boolean isActive;
    @Column(name = "mas_form_is_lock")
    private boolean isLock;
    @ManyToOne
    @JoinColumn(name = "mas_ft_id")
    private FormType formType;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormQuestion> formQuestions = new ArrayList<FormQuestion>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<DocumentRequired> documentRequiredSet = new HashSet<DocumentRequired>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<Application> applications = new ArrayList<Application>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<FormField> formFields = new ArrayList<FormField>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormCharge> formCharges = new ArrayList<FormCharge>(0);
    @Column(name = "mas_form_method")
    private String formMethod;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormFieldGroup> formFieldGroup;
    @Column(name = "mas_form_name_en")
    private String formNameEn;
    @Column(name = "mas_form_name_si")
    private String formNameSi;
    @Column(name = "mas_form_name_ta")
    private String formNameTa;
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "activity")
    private ElgActivity elgActivity;
    @Column(name = "description", nullable = true, length = 1000)
    private String description;

    //getters and setters

}

网络服务接口

@WebService(name = "formWebServicePort", targetNamespace = "http://lk.gov.elg/core/ws/form/")
public interface FormWebService {

    @WebMethod(operationName = "getAllFormsWithDocumentReferences", action = "urn:GetAllFormsWithDocumentReferences")
    @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
    @WebResult(name = "formsWithDocumentReferences")
    List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName);
}

网络服务实现

@SchemaValidation
@WebService(targetNamespace = "http://lk.gov.elg/core/ws/form/", endpointInterface = "lk.gov.elg.core.ws.citizen.FormWebService")
public class FormWebServiceImpl extends SpringBeanAutowiringSupport implements FormWebService {

    private static final Logger logger = LoggerFactory.getLogger(FormWebServiceImpl.class);
    @Autowired
    private FormService formService;

    @Override
    public List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName) {

        List<Form> formList = formService.getAllFormsWithDocRefs(tenantId, categoryName);
        return formList;
    }
}

帮助我克服这个问题。提前致谢。

4

2 回答 2

0

只是一个想法:我们最近通过向 sessionbean 添加一个(EJB3)拦截器方法来克服这个问题(我们使用的是 JBoss7):

@javax.interceptor.AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
        Object result = ctx.proceed();

        // Add code here to inspect the result and initialize all collections.

        return result;
}
于 2013-05-29T07:53:02.053 回答
0

您是否尝试在退出会话之前初始化表单实体的集合。这会导致加载集合。所以基本上它会做同样的急切加载,但不知何故急切加载并不总是有帮助。如果需要,我可以稍后或明天挖掘更多细节。

于 2013-05-28T17:37:01.887 回答