调用Web服务后 - 获取某些属性的空值

时间:2012-11-16 08:25:17

标签: java web-services cxf

我们正在使用CXF网络服务。

情景:

一个服务电话正在退回List<NoteDTO>。 数据存在NoteDTO但在调用Web服务方法后正在撤回List 我们在NoteDTO类中看到了空值。

日志中也没有任何异常。

我的理解是Webservices中存在转换问题。

public class NoteDTO {


    /** The text of the note. */
    private String text;

    /** The date the note was created. */
    private Date created;

    /** The user who created the note. */
    private UserDTO createdBy;

    /** The date the note was last modified. */
    private Date modified;

    /** The last user to modify the note. */
    private UserDTO modifiedBy;


    private String wfStep;  

    public void setWfStep(String wfStep) {
        this.wfStep = wfStep;
    }

    /**
     * Default constructor for JAXB.
     */
    public NoteDTO() {
    }

    /**
     * Constructor taking basic values.
     * @param text The text of the note.
     * @param created The date created.
     * @param createdBy The user who create the note.
     */
    public NoteDTO(String text, Date created, UserDTO createdBy) {
        this.text = text;
        this.created = created;
        this.createdBy = createdBy;     
    }

    /**
     * Getter for the text of the note.
     * @return The text of the note.
     */
    public String getText() {
        return text;
    }

    /**
     * Setter for the text of the note.
     * @param text The text of the note.
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Getter for the created date.
     * @return The created date.
     */
    public Date getCreated() {
        return created;
    }

    /**
     * Setter for the created date.
     * @param created The create date.
     */
    public void setCreated(Date created) {
        this.created = created;
    }

    /**
     * Getter for the modified date.
     * @return The modified date.
     */
    public Date getModified() {
        return modified;
    }

    /**
     * Setter for the modified date.
     * @param modified The modified date.
     */
    public void setModified(Date modified) {
        this.modified = modified;
    }

    /**
     * Getter for the created by user.
     * @return The created by user.
     */
    public UserDTO getCreatedBy() {
        return createdBy;
    }

    /**
     * Setter for the created by user.
     * @param createdBy The created by user.
     */
    public void setCreatedBy(UserDTO createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * Getter for the modified by user.
     * @return The modified by user.
     */
    public UserDTO getModifiedBy() {
        return modifiedBy;
    }

    /**
     * Setter for the modified by user.
     * @param modifiedBy The modified by user.
     */
    public void setModifiedBy(UserDTO modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    /**
     * Getter for the workflow step.
     * @return The workflow step.
     */
    public String getWfstep() {
        return this.wfStep;
    }

我有一个Web服务类

@WebService
public interface NotesService {

    /**
     * Get server notes for a specific workflow instance
     * @param workflowInstanceEntitityId Workflow Instance Entity ID
     * @return A list of notes.
     */
    List<NoteDTO> getNotesForWorkflowInstance(String bundleName, String workflowInstanceName);
}

数据存在于NoteDTO的另一侧,但是在下面的电话之后

notes = notesService.getNotesForWorkflowInstance(bundleName, workflowInstanceName);

我将wfStep属性值设为null

有什么想法吗?

事先坦克斯。

2 个答案:

答案 0 :(得分:0)

可能 @WebMethod 注释会有帮助吗?

答案 1 :(得分:0)

您的类上没有相应的注释,因此我假设您使用的是Java-First方法。

我绝对相信您的NoteDTO不是XSD类型的WSDL架构。如果使用WSDL从java代码生成CXF,则需要在Web服务对象上添加相应的注释。所以你的NoteDTO类应该是这样的:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NoteDTO", propOrder = {
    "text",
    "created",
    "createdBy",
    "modified",
    "modifiedBy",
    "wfStep"
})
public class NoteDTO {

    @XmlElement(name = "text")
    private String text;

    @XmlElement(name = "created")
    private Date created;

    @XmlElement(name = "createdBy")
    private UserDTO createdBy;

    @XmlElement(name = "modified")
    private Date modified;

    @XmlElement(name = "modifiedBy")
    private UserDTO modifiedBy;

    @XmlElement(name = "wfStep")
    private String wfStep;  

}

UserDTO以及您的Web服务调用中使用的任何其他对象相同。那你的界面应该是这样的:

@WebService(targetNamespace = "http://your.target.namespace/", name = "NotesService")
@XmlSeeAlso({ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface NotesService {

    @WebResult(name = "getNotesForWorkflowInstanceResponse", targetNamespace = "http://your.target.namespace/")
    @WebMethod(operationName = "getNotesForWorkflowInstance", action = "http://your.target.namespace/getNotesForWorkflowInstance")
    public List<NoteDTO> getNotesForWorkflowInstance(
        @WebParam(name = "bundleName", targetNamespace = "http://your.target.namespace/") String bundleName,
        @WebParam(name = "workflowInstanceName", targetNamespace = "http://your.target.namespace/") String workflowInstanceName        
    );

}

未经测试,只需手动添加所有内容。阅读有关Java-First方法的更多信息,您将了解自己的错误。这只是一个开始的提示。