使用带有 Joda 日期类型的 Spring 3、ReST 和 XML 时返回数据

发布于 2024-11-07 06:29:36 字数 1113 浏览 4 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-11-14 06:29:36

要回答我自己的问题...以下链接让我走上了正确的道路:http://bdoughan.blogspot.com/2011/05/jaxb-and-joda-time-dates-and-times.html

基本上你必须为每种类型。这是链接中的示例:

package blog.jodatime;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.joda.time.DateTime;

public class DateTimeAdapter 
    extends XmlAdapter<String, DateTime>{

    public DateTime unmarshal(String v) throws Exception {
        return new DateTime(v);
    }

    public String marshal(DateTime v) throws Exception {
        return v.toString();
    }

}

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:

package blog.jodatime;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.joda.time.DateTime;

public class DateTimeAdapter 
    extends XmlAdapter<String, DateTime>{

    public DateTime unmarshal(String v) throws Exception {
        return new DateTime(v);
    }

    public String marshal(DateTime v) throws Exception {
        return v.toString();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文