使用spring发送和接收json数据
问题描述:
我正在使用GOOGLE CHROME的POST MAN客户端发送articleName和articleId AS HEADER应用程序/ json.What我需要在我的控制器和库以及我的spring servlet中进行更改。 xml?我的控制器如下。使用spring发送和接收json数据
公共类ArticleController {
@Autowired
private ArticleService articleService;
Article article = new Article();
Long articleId = article.getArticleId();
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Article saveArticle(@ModelAttribute Article article,
BindingResult bindingresult) {
int a = articleService.addArticle(article);
if (a == 1) {
return new ModelAndView("success");
} else {
return new ModelAndView("error");
}
}
My Spring servlet is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="net.roseindia" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>net.roseindia.model.Article</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
PLZ帮我....预先感谢。
答
我假设你想要的是从Spring轻松返回JSON。
要做到这一点,你需要杰克逊依赖:
public @ResponseBody Article saveArticle(@ModelAttribute Article article,
BindingResult bindingresult) {
....
}
这种方法将返回JSONified:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
当你拥有它, 你可以@ResponseBody
注释就像这个注解你的方法条款对象的回应。
但是我的项目不是一个MVC项目,而是一个动态Web项目,所以无法添加依赖项,因为我没有在这里使用pom.xml。 – user2354150 2013-05-08 07:25:41
因此,只需将它与杰克逊贴在一起,以标准方式添加即可。 – 2013-05-08 07:27:29
但是我在构建路径中添加了jackson mapper-asl-1.9.0以及lib.But即将发生的异常:org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.http.converter。 json.MappingJacksonHttpMessageConverter]: – user2354150 2013-05-08 07:31:14