JAXB为属性返回null

时间:2015-04-17 18:28:44

标签: java eclipse spring maven jaxb

我正在使用Spring-WS开发一个soap web服务。我有这个功能:

@PayloadRoot(namespace = NAMESPACE, localPart = "data")
@ResponsePayload
public Theaters getTheaters(
        @RequestPayload final JAXBElement<TheaterSearch> dataElement)
        throws SQLException {
    TheaterSearch data = dataElement.getValue();
    System.out.println(ReflectionToStringBuilder.toString(dataElement));
    if (data.getMovieId() == null && data.getLocation() == null) {
        throw new IllegalArgumentException();
    } else {
        if (data.getMovieId() == null) {
            return db.getTheaters(data.getLocation());
        } else {
            if (data.getLocation() == null) {
                return db.getTheatersForMovie(data.getMovieId());
            } else {
                Theaters theaters = db.getTheatersForMovie(
                        data.getMovieId(), data.getLocation());
                return theaters;
            }
        }
    }
}

Eclipse中的下一个测试传递,当我从eclipse调用mvn test时(菜单运行为)。奇怪的问题是当我从命令行调用mvn test时它没有通过。 JAXB返回具有null属性的TheaterSearch对象。所以我得到了IllegalArgumentException。我该如何调试这个问题?

@Test
public void TheaterEndpoint() throws Exception {
    Source requestPayload = new StreamSource(new ClassPathResource("data.xml").getInputStream() );
    Source responsePayload = new StreamSource(new ClassPathResource("response.xml").getInputStream());

    mockClient.sendRequest(withPayload(requestPayload)).
            andExpect(payload(responsePayload));
}

data.xml中

<data xmlns="http://iaws/ws/contractfirst/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://iaws/ws/contractfirst/example Request.xsd">
    <movieId>tt0387564</movieId>
    <location>NY</location>
</data>

TheaterSearch

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "data")
public class TheaterSearch {

@XmlElement(name = "movieId", nillable = false)
private String movieId;

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

public TheaterSearch(final String movieId,
        final String location) {
    if (movieId == null && location == null) {
        throw new IllegalArgumentException();
    }
    this.movieId = movieId;
    this.location = location;
}

public final String getMovieId() {
    return movieId;
}
public final String getLocation() {
    return location;
}

}

0 个答案:

没有答案