자바.java.javaClassCastException: javax.xml.bind입니다.JAXB 요소를 캐스트할 수 없습니다.
웹 서비스를 호출하기 위해 Spring boot을 사용하고 있습니다.
구성 클래스는 다음과 같습니다.
@Configuration
public class ClientAppConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("com.client.stub");
return marshaller;
}
@Bean
public ARTestClient arTestClient(Jaxb2Marshaller marshaller) {
ARTestClient client = new ARTestClient();
client.setDefaultUri("uri");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
다음과 같이 서비스를 요청합니다.
OutputMessageType response = (OutputMessageType) getWebServiceTemplate().marshalSendAndReceive(
inputMessageType, new SoapActionCallback("http://Serviceuri"));
다음 오류가 발생합니다.
[2016-03-18 14:45:43.697] boot - 10272 ERROR [http-nio-8080-exec-1] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is java.lang.ClassCastException: javax.xml.bind.JAXBElement
cannot be cast to com.wfs.client.stub.ar.OutputMessageType] with root cause
웹 서비스의 출력을 마샬에서 해제하는 방법???? 응답에 대한 마샬을 어떻게 설정합니까?
재미있게도 저는 같은 문제를 가지고 있었습니다. 제가 한 일은 다음과 같습니다.
에 대한 응답을 보냅니다.
JAXBElement<OutputMessageType>
그래서 그 결과는
JAXBElement<OutputMessageType> response = (JAXBElement<OutputMessageType>) getWebServiceTemplate().marshalSendAndReceive(
inputMessageType, new SoapActionCallback("http://Serviceuri"));
// Then just call
System.out.println(response.getValue());
저는 당신과 거의 같은 구성을 가지고 있습니다.왜 클래스캐스트 예외가 있는지 아직도 알아보고 있습니다.적어도 우리에겐 해결책이 있어요
오늘날에도 관련이 있다면 구성 파일에서 이 문제를 해결할 수 있는 방법이 있습니다..setPackages를 사용하는 대신다음 줄에서 스캔하려면:marshaller.setPackagesToScan("com.client.stub")
.setClassesToBeBound 사용을 고려
그러나 패키지 아래의 각 클래스(마셜링 및 마셜링 해제에 사용됨)를 참조해야 합니다.
marshaller.setClassesToBeBound(new Class[] {
com.client.stub.Foo.class,
com.client.stub.Bar.class,
com.client.stub.Baz.class
});
JAX BElement에 캐스팅할 필요가 없습니다.
JAXBElement<OutputMessageType> response = (JAXBElement<OutputMessageType>)
getWebServiceTemplate().marshalSendAndReceive(
inputMessageType, new SoapActionCallback("http://Serviceuri"));
// Then just call
System.out.println(response.getValue());
내 경우에는 잘 작동했습니다.
이 모든 것은 xsd에서 루트 요소를 정의하는 방법에 따라 달라집니다.다음과 같이 정의할 경우
<element name="myRootElement" type="tns:myRootElementType" />
그런 다음 JAXB 클래스를 생성합니다. 언마샬러는 언마샬링 후 JAXBElement를 다시 실행합니다.루트 요소를 가져오려면 JAXBElement.getValue();
하지만 루트 요소를 다음과 같이 정의하면 -
<element name="myRootElement">
<complextType>
Your definition of complex type - whatever it is
</complexType>
<element>
그런 다음 JAXB 클래스를 생성합니다. 언마셜러는 루트 요소를 나타내는 JAXB 생성 클래스의 전체 개체를 반환합니다.이 경우 JAXBElement 개체를 반환하지 않습니다.
당신은 Unmarshaller의 문서를 확인할 수 있습니다 -
https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.html
구체적으로 섹션 - 전역적으로 선언된 루트 요소와 선언된 유형별로 선언된 루트 요소를 해제합니다.
xsd를 변경한 후에는 JAXB 클래스를 다시 생성해야 합니다.
이것으로 당신의 문제가 해결되기를 바랍니다.
언급URL : https://stackoverflow.com/questions/36088010/java-lang-classcastexception-javax-xml-bind-jaxbelement-cannot-be-cast-to
'programing' 카테고리의 다른 글
일정한 시간이 지난 후에 중단되는 경우 Spring Scheduled 실행 중지 (0) | 2023.07.21 |
---|---|
파이썬에서 "EOF가 아닌 동안"에 대한 완벽한 대응물은 무엇입니까? (0) | 2023.07.21 |
데이터베이스에 "(빈 문자열)을 NULL이 아닌 값으로 저장할 수 있습니까? (0) | 2023.07.21 |
메모장++에서 파이썬 스크립트를 실행하는 방법? (0) | 2023.07.21 |
python 또는 ipython 인터프리터 입력 시 모듈 자동 가져오기 (0) | 2023.07.21 |