java - SpringMVC返回json串
問題描述
在用springmvc返回json串的時候,頁面上只顯示一個鍵值對,如圖
目的是要獲取這個樣子的
{'text':'文本','flag':'文本'}
為什么會出現(xiàn)這種只有第一個鍵值對的情況。
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); }}
在這個代碼的末尾輸出返回值任就是 正確的信息,但是在網(wǎng)頁上顯示就不真確了。
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;@Servicepublic 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('電話號碼已經(jīng)被注冊'); 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; }}
上面是問題代碼。
后來我寫了一個簡單的測試
@RestControllerpublic class StudentInfo { Student student = new Student(); @RequestMapping('/test') public Student getStudentInfo(){student.setUsername('123');student.setPassword('123');student.setTelphone('123');//language=JSONreturn student; }}
卻能正確的返回json
{'username':'123','password':'123','telphone':'123'}
問題解答
回答1:Message缺少:
public String getFlag() { return flag;}
相關(guān)文章:
1. 在應用配置文件 app.php 中找不到’route_check_cache’配置項2. 跨類調(diào)用后,找不到方法3. sql語句 - 如何在mysql中批量添加用戶?4. 怎么php怎么通過數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。5. mysql - 表名稱前綴到底有啥用?6. 編輯成功不顯示彈窗7. wamp中的mySQL可以單獨使用嗎8. 為什么php修改數(shù)據(jù)無法同步到數(shù)據(jù)庫,只是當前頁面修改成功?9. 哭遼 求大佬解答 控制器的join方法怎么轉(zhuǎn)模型方法10. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現(xiàn)存在即更新應該使用哪個標簽?
