使用spring在json中发送和接收数据

时间:2013-05-08 06:52:37

标签: json spring

我正在使用GOOGLE CHROME的POST MAN CLIENT发送articleName和articleId AS HEADER application / json。我需要在我的控制器和库以及我的spring servlet.xml中更改哪些内容?我的控制器如下。

public class 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帮助我....提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为你想要的是从Spring轻松返回JSON。

要做到这一点,你需要杰克逊依赖:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.1</version>
    </dependency>

当你拥有它时, 您可以使用@ResponseBody注释对此方法进行注释,如下所示:

public @ResponseBody Article saveArticle(@ModelAttribute Article article,
        BindingResult bindingresult) {
....
}

这样的方法将返回JSONified Article对象作为响应。