2013년 12월 9일 월요일

spring 에서 json으로 값을 받기

  • Spring 버전을 올리고, JSON Library를 받아야한다.
    pom.xml
    
    
    ...
    <properties>
     <java-version>1.7</java-version>
     <org.springframework-version>3.2.4.RELEASE</org.springframework-version>
     <org.spring-security-version>3.1.4.RELEASE</org.spring-security-version>
     <org.aspectj-version>1.7.1</org.aspectj-version>
     <org.slf4j-version>1.7.5</org.slf4j-version>
    </properties>
    ...
    
    <!-- JSON Library -->
     <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.2.2</version>
     </dependency>
     <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.2.2</version>
     </dependency>
     <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.2.2</version>
     </dependency>
     <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
     </dependency>
     <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
     </dependency>
     <!-- <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> 
      <version>1.0</version> </dependency> -->
     <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.12</version>
     </dependency>
     <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.2.4</version>
     </dependency>
    ...
    
    
  • root-context.xml 에 추가
  • root-context.xml
    
    
    ...
     <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
     <bean name="restTemplate" class="org.springframework.web.client.RestTemplate">
      <property name="messageConverters">
       <list>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
         <property name="supportedMediaTypes" value="text/html,text/plain,application/xml" />
        </bean>
       </list>
      </property>
     </bean>
    ...
    
  • Exam01Controller.java
    • Exam01Controller.java
    /** * Simply JSON */ @RequestMapping(value = "/json", method = RequestMethod.GET) @ResponseBody public Monitoring json(Monitoring monitoring) { logger.info("Welcome exam01 json"); monitoring.setClassName("aaa"); monitoring.setId(1l); monitoring.setJobName("bb"); return monitoring; }

  • url 호출

댓글 없음:

댓글 쓰기