在Spring3.1之后,如果使用<mvc:annotation-driven />,即使用注解驱动,默认情况下已经配置了MappingJackson2HttpMessageConverter,那么只要加入对应的实现jar包即可:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.5</version>
</dependency>

  如果没有使用<mvc:annotation-driven />,则需要手动配置MessageConverter:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
        </list>
    </property>
</bean>

  接下来使用@ResponseBody来返回json数据:

@RequestMapping(value="show", method=RequestMethod.GET)
public @ResponseBody User show(){
    User user = new User();
    user.setUsername("123");
    user.setPassword("123");
    user.setBirth(new Date());
    return user;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2021-12-02
  • 2021-07-28
  • 2021-10-03
  • 2021-06-12
猜你喜欢
  • 2021-05-13
  • 2022-02-01
  • 2021-09-25
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
相关资源
相似解决方案