java:spring rest WS - 无法找到要在模型中编组的对象

时间:2011-01-17 12:25:19

标签: spring rest jaxb

尝试使用jaxb

在spring mvc 3.0中运行rest ws时获取异常
  

javax.servlet.ServletException:无法在模型中找到要编组的对象:   {org.springframework.validation.BindingResult.employees = org.springframework.validation.BeanPropertyBindingResult:0 errors,employees = spring3.rest.bean.EmployeeList@13d1402}

我的rest-servlet.xml文件包含jaxb条目,如下所示

<bean id="employees" class="org.springframework.web.servlet.view.xml.MarshallingView">
 <constructor-arg ref="jaxbMarshaller" />
</bean>

 <bean id="employeeController" class="spring3.rest.controller.EmployeeController">
  <property name="employeeDS" ref="employeeDS" /> 
  <property name="jaxb2Mashaller" ref="jaxbMarshaller" />
 </bean>

EmployeeController是:

@Controller
public class EmployeeController {

private EmployeeDS employeeDS;

public void setEmployeeDS(EmployeeDS ds) {
    this.employeeDS = ds;
}

private Jaxb2Marshaller jaxb2Mashaller;

public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
    this.jaxb2Mashaller = jaxb2Mashaller;
}

@RequestMapping(method=RequestMethod.GET, value="/employee/{id}")
public ModelAndView getEmployee(@PathVariable String id) {
    Employee e = employeeDS.get(Long.parseLong(id));
    return new ModelAndView("employees", "object", e);
}

在rest-servlet.xml中

               spring3.rest.bean.EmployeeList          spring3.rest.bean.Employee       

并且这两个bean都使用@XmlRootElement

进行了注释

2 个答案:

答案 0 :(得分:3)

遇到类似的问题,事实证明在我的情况下我返回List(A)而类型'A'本身被声明为@XmlRootElement,我忽略了我返回类型A的List的事实,有一个包装器对象定义为保存列表并将其声明为@XMLRoolElement,能够继续前进。

认为帖子陈旧,可能有助于了解。

答案 1 :(得分:0)

您需要将响应对象放入Model

@RequestMapping(value = "/myPath", method = RequestMethod.GET)
public @ResponseBody
MyObjct getMyObject(ModelMap model) {
    MyObjct t = new MyObjct();
    model.addAttribute(t); // put into model
    return t;
}