0

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

公共类文章控制器 {

    @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>

请帮帮我....提前谢谢。

4

1 回答 1

0

我假设您想要的是从 Spring 轻松返回 JSON。

为此,您需要 Jackson 依赖项:

    <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 对象作为响应。

于 2013-05-08T07:06:20.463 回答