一個簡單的springboot的案例實現一個請求控制層返回一個JSON
阿新 • • 發佈:2019-01-23
package com.yunst.demo.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunst.demo.domain.User;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/{id}")
public User view(@PathVariable("id") Long id) {
User user = new User();
user.setId(id);
user.setName("張三丰");
user.setAge(26);
user.setSex('男');
user.setAddress("湖北省武當山");
return user;
}
}
3:編寫實體類user
package com.yunst.demo.domain;
/**
*
* @author admin
*
*/
public class User {
private Long id;
private String name;
private int age;
private char sex;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunst.demo.domain.User;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/{id}")
public User view(@PathVariable("id") Long id) {
User user = new User();
user.setId(id);
user.setName("張三丰");
user.setAge(26);
user.setSex('男');
user.setAddress("湖北省武當山");
return user;
}
}
3:編寫實體類user
package com.yunst.demo.domain;
/**
*
* @author admin
*
*/
public class User {
private Long id;
private String name;
private int age;
private char sex;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}