zoukankan      html  css  js  c++  java
  • spring mvc 返回Json

     

     

    1 pom文件中 添加

     

    spring 框架的jar 4.1.2 版本

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>3.8.1</version>

    <scope>test</scope>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>4.1.2.RELEASE</version>

    </dependency>

     

    jackson json jar

    <dependency>

    <groupId>org.codehaus.jackson</groupId>

    <artifactId>jackson-mapper-asl</artifactId>

    <version>1.9.2</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->

    <dependency>

    <groupId>org.codehaus.jackson</groupId>

    <artifactId>jackson-core-asl</artifactId>

    <version>1.9.13</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.9.3</version>

    </dependency>

     

    springmvc-servlet.xml 配置

    好多人说需要加入以下配置,但是我没加,也是运行正常,能返回jason格式的数据

    <bean id="mappingJacksonHttpMessageConverter"

    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

    <property name="supportedMediaTypes">

    <list>

    <value>application/json;charset=UTF-8</value>

    </list>

    </property>

     

     

    <?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:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"

    >

     

    <!-- scan the package and the sub package -->

    <context:component-scan base-package="com.maven01.*"/>

    <!-- don't handle the static resource -->

    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->

    <mvc:annotation-driven />

    <!-- 对静态资源的处理 ,不需要dispatcher servelet-->

    <mvc:resources mapping="/static/**" location="/static/"/>

    <!-- configure the InternalResourceViewResolver -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

    id="internalResourceViewResolver">

    <!-- 前缀 -->

    <property name="prefix" value="/WEB-INF/view/" />

    <!-- 后缀 -->

    <property name="suffix" value=".jsp" />

    </bean>

    </beans>

     

    controller 的代码

    package com.maven01.controller;

     

    import java.util.ArrayList;

    import java.util.List;

     

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.bind.annotation.ResponseBody;

     

    import com.maven01.dto.*;

    @Controller

    @RequestMapping("/mvc")

    public class DemoController {

     

    @RequestMapping("/index")

    public String hello(){

    return "index";

    }

    @RequestMapping(method=RequestMethod.GET, value="/getUser",produces="application/json")

    public @ResponseBody List<UserDto> getUser(){

    ArrayList<UserDto> resultList=new ArrayList<UserDto>();

    for(int i=0;i<100;i++)

    {

    UserDto user=new UserDto(i,String.format("hbb0b0-%s", i+""));

    resultList.add(user);

    }

    return resultList;

    }

    }

     

     

    运行效果

     

     

  • 相关阅读:
    使用nginx在本地查看angular打包项目
    iso与安卓遇到的问题
    Spark 常用的读取数据api
    Spark DataFrame常用API
    spark 词频统计
    spark-shell和spark-sql
    Spark中 RDD、DF、DS的区别与联系
    SparkSQL连接Hive
    spark安装 centos7
    scala安装 centos7
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/8288714.html
Copyright © 2011-2022 走看看