我已经为我的XML un / marshalling和domain实体使用单个实体设置了测试Spring webservice。
我正在使用maven和嵌入式Jetty以及嵌入式H2数据库来测试服务。
当我运行POST来创建“注册”时,我可以在我的Web控制台中看到以下信息:
Hibernate:插入注册(id,email,firstName,language,lastName)值(null,?,?,?,?)
如您所见,Object中的字段为空,因此插入失败。
对细节表示道歉,但如果有人能够回答这个问题,我想你需要以下所有信息:
弹簧context.xml中
<bean="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
p:marshaller-ref="jaxb" p:unmarshaller-ref="jaxb" />
<oxm:jaxb2-marshaller id="jaxb">
<oxm:class-to-be-bound name="com.nosh.project.bean.Registration" />
</oxm:jaxb2-marshaller>
<context:component-scan base-package="com.nosh.project.controller" />
<context:component-scan base-package="com.nosh.project" />
<jdbc:embedded-database id="dataSource" type="H2" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource" p:lobHandler-ref="defaultLobHandler"
p:packagesToScan="com.nosh.project.bean">
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:annotation-driven proxy-target-class="true"/>
Registration.java(实体和XML对象)
@XmlRootElement(name="registration")
@Entity
public class Registration implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String firstName;
private String lastName;
private String email;
private String language;
public Registration() {
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@XmlElement(name = "firstName", required = true)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement(name = "lastName", required = true)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlElement(name = "email", required = true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@XmlElement(name = "language", required = true)
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
控制器
@Controller
@RequestMapping("/registration")
@Transactional()
public class RegistrationController {
private RegistrationDao registrationDao;
@Autowired
public void setRegistrationDao(RegistrationDao registrationDao) {
this.registrationDao = registrationDao;
}
//TODO add inbound object validation to check all required fields are correct.
@RequestMapping(method = RequestMethod.POST)
@Transactional(readOnly = false)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody
Registration saveRegistration(Registration registration,
BindingResult result, HttpServletResponse response) throws BindException {
if (result.hasErrors()){
throw new BindException(result);
}
registrationDao.saveRegistration(registration);
response.setHeader("Location", "/Registration/" + registration.getId());
return registration;
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Registration getRegistration(@PathVariable("id") Long registrationId) {
return registrationDao.getRegistration(registrationId);
}
}
我看了几个例子,我看不到我在这里缺少的东西。是否与我使用单个对象进行数据实体和xml解组这一事实有关?
非常感谢
答案 0 :(得分:0)
此处是远景,但您可能需要为supportedMediaTypes
定义marshallingConverter
,然后确保客户端正在发送正确的Content-Type
标头。这是我用的......
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxb" />
<property name="supportedMediaTypes" value="application/xml"/>
<property name="marshaller" ref="jaxb" />
<property name="unmarshaller" ref="jaxb" />
</bean>