springmvc返回自定义对象的json字符串

1、https://blog.csdn.net/xuanyonghao/article/details/73928497
2、https://www.cnblogs.com/cl1255674805/p/5486950.html

具体实现:
1、引入包:

<jackson.version>2.9.5</jackson.version>
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
    </dependency>

2、

<!-- 通过实体类返回json格式数据的关键配置 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <!--返回字符串格式json-->
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

3、页面(重点是dataType是json):

$.ajax({
                            //url: "/HospitalManage/data/patient.json",
                            url: "/hos/manager/getPatientList",
                            async: true,
                            dataType: "json",
                            success: function(result) {
                                self.patient_info = result.data;
                                console.log("length:"+result.data.length)
                                self.$nextTick(function() {
                                    self.laypage.render({
                                        elem: 'pages',
                                        curr: self.page,
                                        limit: self.pageSize,
                                        count: result.data.length,
                                        jump: function(obj, first) {
                                            if(!first) {
                                                self.page = obj.curr;
                                                self.getPatientInfo();
                                            }
                                        }
                                    });


                                });
                            }
                        });

4、后台Controller:

    @ResponseBody
    @RequestMapping("/getPatientList")
    public Result getPatientList(){
        Result result = new Result();
        TPatientExample example = new TPatientExample();
        example.createCriteria().andIdIsNotNull();
        List<TPatient> patientList = patientService.getPatientList(example);
        result.setData(patientList);

        return result;
        //return result.toString();//接收ajax请求,返回一个json数组
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值