類的方法以json格式輸出
1.新增model層,在model層建立User類
包名:com.huinongtx.demospringboot.model
類名:User
類內容:
package com.huinongtx.demospringboot.model;
/**
* Created by dengdashuai on 2018/12/6.
*/
public class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public String getPassWord() {
return passWord;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
2.修改HelloWorldControl這個類
包名:com.huinongtx.demospringboot.web
類名:HelloWorldControl
類內容:
package com.huinongtx.demospringboot.web;
import com.huinongtx.demospringboot.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by dengdashuai on 2018/12/6.
*/
// 類的方法以json格式輸出
@RestController
public class HelloWorldControl {
// http://localhost:8080/getuser請求對映到getUser方法
@RequestMapping("/getuser")
public User getUser() {
User user = new User();
user.setUserName("daji");
user.setPassWord("daji1206");
return user;
}
}
3.執行DemospringbootApplication中的Main方法
package com.huinongtx.demospringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemospringbootApplication {
public static void main(String[] args) {
SpringApplication.run(DemospringbootApplication.class, args);
}
}
4.在瀏覽器輸入http://localhost:8080/getuser