2013-05-08 6 views
0

GOOGLE CHROME의 POST MAN CLIENT를 사용하여 articleName을 보내고 articleId AS HEADER application/json을 사용하고 있습니다. 컨트롤러 및 라이브러리뿐만 아니라 스프링 서블릿에서 변경해야 할 사항은 무엇입니까? 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은 사전에 .... 감사 나 좀 도와.

답변

0

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 주석으로 방법을 주석을 달 수 있습니다 :

는 잭슨 의존성이 필요하다는 것을 수행하려면 응답으로 기사 개체.

+1

하지만 내 프로젝트는 MVC 프로젝트가 아니라 동적 웹 프로젝트이므로 여기에 pom.xml이 없으므로 종속성을 추가 할 수 없습니다. – user2354150

+0

Jackson과 함께 jar를 붙임으로써 표준 방식으로 추가하십시오. –

+0

하지만 org.springframework.beans.BeanInstantiationException : org.springframework.http.converter 인스턴스를 생성 할 수 없습니다. lib.But 예외는 물론 경로를 빌드 할 때 jackson mapper-asl-1.9.0을 추가했습니다. json.MappingJacksonHttpMessageConverter] : – user2354150