我在我的应用程序中使用了Spring OXM和JiBX。
下面的是我的Spring配置文件
<context:component-scan base-package="com.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<oxm:jibx-marshaller target-class="com.request.RequestClass" id="rqMarshaller"/>
<oxm:jibx-marshaller target-class="com.response.ResponseClass" id="rsMarshaller"/>
<bean id="xmlViewer" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="rsMarshaller" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
以下是控制器类
@Controller
public class MyController {
@Autowired
private JibxMarshaller rqMarshaller;
@RequestMapping(value = "/myrequest", method = RequestMethod.POST)
public ModelAndView searchFlights(@RequestBody String request) {
System.out.println("Inside");
System.out.println("request = "+request);
Source source = new StreamSource(new StringReader(request));
RequestClass rq = null;
try {
rq = (RequestClass) rqMarshaller.unmarshal(source);
} catch (XmlMappingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ResponseClass e = new ResponseClass();
e.setVersion("2.0");
Orig ond = new Orig();
ond.setCode("AIT");
e.getOrig().add(ond);
return new ModelAndView("xmlViewer","object",e);
}
}
当我发送XML请求时,它已成功编组,但对于响应,我收到了以下错误消息。
org.jibx.runtime.JiBXException: No marshaller defined for class com.response.ResponseClass
我已经在spring配置文件中为ResponseClass
定义了marshaller。
请帮忙。感谢。
答案 0 :(得分:0)
最后我想出了解决方案!!!
注册bindingName
时需要指定JiBxMarshaller
属性。
<oxm:jibx-marshaller target-class="com.request.RequestClass" id="rqMarshaller" bindingName="rqBinding"/>
<oxm:jibx-marshaller target-class="com.response.ResponseClass" id="rsMarshaller" bindingName="rsBinding/>
并在JiBX的相应绑定/映射文件中指定相同的名称。
就是这样!