我有太多问题1:debbuging restTemplate,2:将xml映射到pojo。
这是我的代码 POJO:
@XmlRootElement(name = "parent")
public class Parent {
private User user;
public Parent(){
}
//getter setter
}
另一个Pojo
@XmlRootElement(name = "user")
public class User {
public User(){
}
private long id;
private String name;
private Date registrationDate;
}
我有另一个web服务,它将xml数据返回为:
<parent>
<user id="23" name="name">
<registrationdate>2012-02-27T13:08:31.771-05:00</registrationdate>
</user>
</parent>
我使用Spring 3并重新模板(在我的类路径中我有jaxb-api和jaxb-impl): 在我的appilacation-context中,我有:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
在我的服务层我有:
@Service
public class ParentServiceI implements ParentService{
Logger logger = Logger.getLogger(this.getClass());
@Autowired
private RestTemplate restTemplate;
public Parent getMoreInfo() {
logger.info("getting info");
Parent p = restTemplate.getForObject("http://localhost:3128/dadad.xml", Parent.class);
logger.info(p.toString());
return p;
}}
我的第一个问题: 当我启动这个代码时,我当然遇到了映射问题,但我无法调试,我无法在我的控制台中看到任何错误日志,任何异常,我只能得到它:
09:31:50,503 INFO 959993440@qtp159257116-0 ParentServiceI :64 - getting info
09:31:50,670 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Created GET request for "http://localhost:3128/dadad.xml"
09:31:50,971 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:520 - Setting request Accept header to [application/xml, text/xml, application/*+xml, application/json]
09:31:58,762 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:465 - GET request for "http://localhost:3128/dadad.xml" resulted in 200 (OK)
09:31:58,764 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Reading [com.mypackage.Parent] as "text/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@7d6d4e3e]
就是这样。没有错误,代码只是卡在那里。 我得到第一条日志消息“获取信息”,我没有得到第二条日志消息 我该怎么调试?
我的第二个问题: 设置pojo后,我得到了一些结果:
09:31:50,503 INFO 959993440@qtp-159257116-0 ParentServiceI :64 - getting info
09:31:50,670 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Created GET request for "http://localhost:3128/dadad.xml"
09:31:50,971 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:520 - Setting request Accept header to [application/xml, text/xml, application/*+xml, application/json]
09:31:58,762 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:465 - GET request for "http://localhost:3128/dadad.xml" resulted in 200 (OK)
09:31:58,764 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Reading [com.mypackage.Parent] as "text/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@7d6d4e3e]
09:31:59,337 INFO 959993440@qtp-159257116-0 serviceI.EquipmentServiceI:83 - Parent [user=User [id=0, name=null, registrationDate=Mon Feb 27 13:08:31 EST 2012]]
everthing是好的映射? 我该如何解决?
谢谢
答案 0 :(得分:4)
我没有找到如何解决我的第一个版本。
映射那个xml文件的属性,我只需要在getter上添加一个注释
@XmlRootElement(name = "user")
public class User {
public User(){
}
private long id;
private String name;
private Date registrationDate;
@XmlAttribute(name="name")
public String getName() {
return name;
}
}
答案 1 :(得分:2)
在您的应用程序上下文文件中添加以下条目以使用jaxb进行编组/解组
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>your.package.name.Parent</value>
<value>your.package.name.User</value>
</list>
</property>
</bean>
此外,您必须通过设置messageConverters
属性告诉您的其余模板使用此marshaller - 就像这样[从here复制的示例代码]:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="messageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
</list>
</property>
</bean>