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.5jackson.version>
<dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>${jackson.version}version>
    dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson.version}version>
    dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>${jackson.version}version>
    dependency>

2、


    <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>
                            
                            <value>application/json;charset=UTF-8value>
                        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 patientList = patientService.getPatientList(example);
        result.setData(patientList);

        return result;
        //return result.toString();//接收ajax请求,返回一个json数组
    }

你可能感兴趣的:(解决问题)