@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
查询视频列表Controller代码:
@RestController
//请求路径
@RequestMapping("/api/v1/pub/video")
public class VideoController {
@Autowired //自动装配
VideoService videoService;
//获取内存中的数据
@RequestMapping(value = "GetVideoList",method = RequestMethod.GET)
public Object GetVideoList() throws JsonProcessingException {
return JsonData.buildSuccess(videoService.GetVideoList());
}
使用RestController和Controller的区别:
如果你想让方法有返回值且是对象,或者map类型数据时,使用Controller不行,必须在方法上加上RequestBody注解。而使用RestController就不需要。
视频类 Video:
public class Video {
private int id;
private String title; //视频标题
private int price; //视频价格
// @JsonProperty("cover_img") 给该属性取别名
private String coverImg; //视频存放图片地址
@JsonFormat(pattern = "yyyy-mm-dd hh:mm:ss",locale = "zh",timezone = "GMT+8") // 指定时间属性输出格式
private Date createTime; //创建的时间
//获取章节数组
@JsonInclude(JsonInclude.Include.NON_NULL) //筛选为空的,不显示
List chapters;
public Video(){}
public Video(int id, String title,Date createTime) {
this.id = id;
this.title = title;
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getCoverImg() {
return coverImg;
}
public void setCoverImg(String coverImg) {
this.coverImg = coverImg;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Video{" +
"id=" + id +
", title='" + title + '\'' +
", price=" + price +
", coverImg='" + coverImg + '\'' +
", createTime=" + createTime +
", chapters=" + chapters +
'}';
}
public List getChapters() {
return chapters;
}
public void setChapters(List chapters) {
this.chapters = chapters;
}
}
VideoService接口:
public interface VideoService {
/**
* 获取Video内存中,所有生成的数据
*
*/
Object GetVideoList();
}
实现类:
@Service
public class VideoServiceImpl implements VideoService {
@Autowired
VideoMapper videoMapper;
@Override
public Object GetVideoList() {
return videoMapper.listVideo();
}
}
VIdeoMapper类,此时里面的数据仅仅是用来测试,存放在内存中:
@Repository //创库
public class VideoMapper {
//使用内存来存取数据,并没有整合mybatis数据测试
private static Map VideoMap = new HashMap<>();
//生产数据
static{
VideoMap.put("Java 基础课程",new Video(1,"Java 基础课程",new Date()));
VideoMap.put("Spring2.0基础到实战",new Video(2,"Spring2.0基础到实战",new Date()));
VideoMap.put("微服务SpringCloud全家桶",new Video(3,"微服务SpringCloud全家桶",new Date()));
}
//查看VideoMap中所有的数据
public Object listVideo(){
//将Map转换成Video类型数组
List
SpringBoot的运行类:
//使用SpringBoot启动注解
@SpringBootApplication
public class DemoProjectApplication {
public static void main(String[] args){
SpringApplication.run(DemoProjectApplication.class);
}
}
/**
* ⼩滴课堂 接⼝返回⼯具类
*/
public class JsonData {
private int code;
private Object data;
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public JsonData(){}
public JsonData(int code, Object data){
this.code = code;
this.data = data;
}
public JsonData(int code, Object data, String msg){
this.code = code;
this.data =data;
this.msg = msg;
}
public static JsonData buildSuccess(Object data){
return new JsonData(0,data);
}
public static JsonData buildError(String msg){
return new JsonData(-1,"",msg);
}
public static JsonData buildError(String msg,int code){
return new JsonData(code,"",msg);
}
}
返回成功JSON结果:
{
"code":0,
"dada":"data对象",
msg:
}
POST请求-form表单
@Data
public class User {
/**
* User
*/
//使用lombok.Data注解,不用写get和set方法
private String username;
@JsonIgnore //隐藏该属性
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private User(){}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
//RequestBody会返回方法返回值
@RestController()
@RequestMapping("/api/v1/pub/user")
public class UserControll {
/**
* 使用POST获取User
*/
//@RequestMapping(value = "login",method = RequestMethod.POST)
UserService userService = new UserServiceImpl();
@PostMapping("login")
public JsonData Loging(@RequestBody User user){ //加入RequestBody可以把Json数据映射到对象中,包括数据以及Map
String result_login = userService.login(user);
if (result_login !=null){ //说明登录成功
System.out.println("登录成功");
return JsonData.buildSuccess(result_login);
}else {
System.out.println("登录失败");
return JsonData.buildError("","login_error");
}
}
}
UserService接口:
public interface UserService {
/**
* 登录成功之后,返回一个token。然后
* @param user
* @return
*/
String login(User user);
}
实现类:
@Service
public class UserServiceImpl implements UserService {
UserMapper userMapper = new UserMapper();
//用户登录成功之后,存放的token,但在公司中存放token不应该存放在内存中。
private static Map sessionMap = new HashMap<>();
@Override
public String login(User user) {
User login = userMapper.login(user.getUsername(), user.getPassword());
if (login ==null){ //登录失败就返回空
return null;
}else {
//登录成功,随机创建一个token
String token = UUID.randomUUID().toString();
System.out.println(token);
return token;
}
}
}
UserMapper:
public class UserMapper {
//使用内存来存取数据,并没有整合mybatis数据测试
private static Map UserMap = new HashMap<>();
//生产数据
static{
UserMap.put("xiaoliu",new User("xiaoliu","123456"));
UserMap.put("xiaochang",new User("xiaochang","123456"));
UserMap.put("xiaochen",new User("xiaochen","123456"));
}
/**
* 返回空登录失败。返回对象有值说明登录成功
* @param name
* @param password
* @return
*/
public User login(String name,String password){
User user = UserMap.get(name);
if (user==null){ //如果为空,说明账号或密码错误,就返回给实现类null
return null;
}
if (user.getPassword().equals(password) ){
return user;
}
return null;
}
}
简介:详解JSON对象提交,批量插入接口开发
2.注解:@PostMapping = @ReqeustMapping(method = RequestMethod.POST)
开发功能:新增视频JSON对象,章数组提交
具体实现咋第二集有实现!!!
//将一组VideoList对象序列化成字符创
List