java - SpringMVC返回json串
天蓬老师
天蓬老师 2017-04-18 10:50:30
0
1
588

在用springmvc返回json串的时候,页面上只显示一个键值对,如图

目的是要获取这个样子的

{"text":"文本","flag":"文本"}

为什么会出现这种只有第一个键值对的情况。

contraller

import com.dcxm.stu.bean.Message;
import com.dcxm.stu.bean.Student;
import com.dcxm.stu.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/student")
public class RegisterController {

    private Student student = new Student();

    @Autowired
    private RegisterService registerService;

    @RequestMapping(value = "/register")
    public Message register(HttpServletRequest request, Model model) {
        student.setTelphone("13245648");
        student.setPassword("123465");
        System.out.println(registerService.studentRegister(student));
        return registerService.studentRegister(student);
    }
}

在这个代码的末尾输出返回值任就是 正确的信息,但是在网页上显示就不真确了。

RegisterService

import com.dcxm.stu.bean.Message;
import com.dcxm.stu.bean.Student;
import com.dcxm.stu.dao.impl.RegisterDaoImpl;
import com.dcxm.stu.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterDaoImpl registerDao;

    public Message studentRegister(Student student) {
        String telphoneNew = student.getTelphone();
        System.out.println("new Tel " + telphoneNew);
        Message message = new Message();
        int row = registerDao.insertInfo(student);
        if (row == 1) {
            message.setText("注册成功");
            message.setFlag("true");
        } else {
            message.setText("电话号码已经被注册");
            message.setFlag("false");
        }
        return message;
    }
}

Message

public class Message {

    private String flag;
    private String text;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
    
    public Message() {

    }

    public Message(String text, String flag) {
        this.flag = flag;
        this.text = text;
    }

    @Override
    public String toString() {
        return text + "   " + flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }
}

上面是问题代码。

后来我写了一个简单的测试

@RestController

public class StudentInfo {

    Student student = new Student();
    @RequestMapping("/test")
    public Student getStudentInfo(){
        student.setUsername("123");
        student.setPassword("123");
        student.setTelphone("123");
        //language=JSON
        return student;
    }
}

却能正确的返回json

{"username":"123","password":"123","telphone":"123"}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(1)
Peter_Zhu

Message缺少:

public String getFlag() {
    return flag;
}
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage