我正在使用spring框架3.1(使用hibernate),我正在尝试生成一个XML表示形式,如:
<user>
<iduser>1</iduser>
<email>bla@hello.com</email>
<firstName>bob</firstName>
</user>
来自这个java类:
@Entity
public class User {
@GenericGenerator(name = "table-hilo-generator", strategy = "org.hibernate.id.IncrementGenerator")
@GeneratedValue(generator = "table-hilo-generator")
@Id
@Column(name = "iduser", unique = true, nullable = false)
private int iduser;
@NotBlank
@NotNull
@NotEmpty
@Length(max = EMAIL_MAX_SIZE)
@Column(name = "email", nullable = false)
private String email;
@NotBlank
@NotNull
@NotEmpty
@Length(max = FIRST_NAME_MAX_SIZE)
@Column(name = "firstName", nullable = false)
private String firstName;
}
@Entity
public class User {
@GenericGenerator(name = "table-hilo-generator", strategy = "org.hibernate.id.IncrementGenerator")
@GeneratedValue(generator = "table-hilo-generator")
@Id
@Column(name = "iduser", unique = true, nullable = false)
private int iduser;
@NotBlank
@NotNull
@NotEmpty
@Length(max = EMAIL_MAX_SIZE)
@Column(name = "email", nullable = false)
private String email;
@NotBlank
@NotNull
@NotEmpty
@Length(max = FIRST_NAME_MAX_SIZE)
@Column(name = "firstName", nullable = false)
private String firstName;
}
我的servlet-conf.xml在ContentNegotiatingViewResolver中包含此视图:
但是我不明白为什么结果是一个带有数百个元素的奇怪的xml:
<!-- XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry key="user123" value="com.....entities.User" />
</map>
</property>
</bean>
</constructor-arg>
</bean>
1 - 可能是编组人员在反射中玩得太多,我怎样才能获得我想要的预期结果? (2 - 我也对使用用户列表生成XML文件感兴趣)我该怎么做?
答案 0 :(得分:0)
就像你已经注意到的那样,发生了什么是因为你没有指定需要序列化的显式模型键,它正在序列化第一个非空值模型对象,在这种情况下发生BindingResult
(用于保持模型中的绑定/验证错误)。您可以进行一些修复:
一个。为您的编组视图指定确切的modelKey
,这应该可行,并将模型设置为特定的模型键:
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
...
</property>
<property name="modelKey" value="command"/>
</bean>
model.addAttribute("command", mymodel);
湾更好的解决方法,恕我直言可以在Spring中使用http转换器,这样您就可以从请求映射方法返回您的对象,使用@ResponseBody
注释它,Spring将负责将对象转换为线程reprsentation( xml或json等),你只需要注册正确的转换器:
@RequestMapping(...)
public @ResponseBody User myMethod(Model model){
return user;
}
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters register-defaults="false"> <!-- you may have to explicitly register other converters though-->
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
</property>
</bean>
</mvc:message-converters>
答案 1 :(得分:0)
我在XStream中遇到了同样的错误,并通过应用以下更改解决了这个问题:
您可以向Marshaller bean添加“supportedClasses”属性,而不是添加modelKey:
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="supportedClasses">
<list>
<value>com.rest.example.model.User</value>
</list>
</property>
</bean>
其次,类User应该使用XStream别名注释,否则您将获得XML中的完整包名称 - 例如:<com.rest.example.model.User>
而不是<user>
- 修复它的方法是:
@XStreamAlias("user")
public class User {
...