使用带有 Joda 日期类型的 Spring 3、ReST 和 XML 时返回数据
我在使用 Spring 3 和 MVC 构建的简单 Web 服务中遇到了一个奇怪的问题。 Web 服务工作正常,我得到了 XML,就像我想要的那样,但是,所有 Joda 日期/时间类型的所有值都是空的。
所以我有一个类似这样的 UserDTO:
@XmlRootElement(name = "user")
public class UserDTO
{
private String firstname;
private String lastname;
private LocalDate birthdate;
...
我有一个类似的控制器:
@Controller
public class UserController
{
@RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
public UserDTO getUser()
{
UserDTO userDTO = new UserDTO();
userDTO.setFirstname("Foo");
userDTO.setLastname("Bar");
userDTO.setBirthdate(new LocalDate(1980,1,1));
return userDTO;
}
}
我得到以下 XML 返回:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
<firstname>Foo</firstname>
<lastname>Bar</lastname>
<birthdate />
</user>
如果我将“Accept”标头更改为 application/json,我确实会得到日期值
{"userVO":{ "First Name","lastname":"Last Name","birthdate":[1978,12,5]}}
关于这可能是什么的任何想法?
I am having an odd problem in a simple web service built with Spring 3 and MVC. The web service works ok and I get XML back just like I want, however, all the values for all Joda date/time types are empty.
So I have a UserDTO which is something like this:
@XmlRootElement(name = "user")
public class UserDTO
{
private String firstname;
private String lastname;
private LocalDate birthdate;
...
And I have a controller like:
@Controller
public class UserController
{
@RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
public UserDTO getUser()
{
UserDTO userDTO = new UserDTO();
userDTO.setFirstname("Foo");
userDTO.setLastname("Bar");
userDTO.setBirthdate(new LocalDate(1980,1,1));
return userDTO;
}
}
I get the following XML back:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
<firstname>Foo</firstname>
<lastname>Bar</lastname>
<birthdate />
</user>
If I change the 'Accept' header to application/json, I do get the date value
{"userVO":{"First Name","lastname":"Last Name","birthdate":[1978,12,5]}}
Any ideas on what this could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答我自己的问题...以下链接让我走上了正确的道路:http://bdoughan.blogspot.com/2011/05/jaxb-and-joda-time-dates-and-times.html
基本上你必须为每种类型。这是链接中的示例:
To answer my own question... the following link set me on the right path: http://bdoughan.blogspot.com/2011/05/jaxb-and-joda-time-dates-and-times.html
Basically you have to create an XmlAdapter for each type. Here is an example from the link: