spring3 mvc restful 返回xml时报406错误,返回json就没有问题。

spring3 mvc restful webservice 时返回的是xml,在dto上加@XmlRootElement注解,如果是直接返回dto是没有问题的,如果是dto的集合就会报406错误,要是返回json就没有问题,另外返回xml是,dto中有属性时间,怎样设定时间格式化啊,返回json可以用注解@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00"),xml怎么做呢。
返回xml 使用 Jaxb2RootElementHttpMessageConverter
返回json 使用 MappingJackson2HttpMessageConverter

在xml返回值中格式化日期可以使用 JAXB 的 @XmlJavaTypeAdapter 注解。例如:

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;

public class Dto {
  @XmlJavaTypeAdapter(DateAdapter.class)
  private Date date;
}

// DateAdapter class
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateAdapter extends XmlAdapter<String, Date> {
  private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  @Override
  public String marshal(Date date) throws Exception {
    return dateFormat.format(date);
  }

  @Override
  public Date unmarshal(String date) throws Exception {
    return dateFormat.parse(date);
  }
}

在使用 Jaxb2RootElementHttpMessageConverter 返回xml时,需要在DTO类上加 @XmlRootElement 注解。