1.日期&json&路径参数接收
2.统一响应结果封装类
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
package com.inmind.controller;
|
package com.inmind.controller;
|
||||||
|
|
||||||
import com.inmind.pojo.User;
|
import com.inmind.pojo.User;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -67,4 +67,33 @@ public class RequestController {
|
|||||||
System.out.println(hobby);
|
System.out.println(hobby);
|
||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
接收日期参数
|
||||||
|
*/
|
||||||
|
@RequestMapping("/dateParam")
|
||||||
|
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
|
||||||
|
System.out.println(updateTime);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
接收json数据
|
||||||
|
* */
|
||||||
|
@RequestMapping("/jsonParam")
|
||||||
|
public String jsonParam(@RequestBody User user){
|
||||||
|
System.out.println(user);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
接收路径参数
|
||||||
|
*/
|
||||||
|
@RequestMapping("/path/{id}/{name}")//注意:路径参数与形参名保持一致,才能接收数据
|
||||||
|
public String pathParam(@PathVariable Integer id,@PathVariable String name){
|
||||||
|
System.out.println(id);
|
||||||
|
System.out.println(name);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,75 @@
|
|||||||
|
package com.inmind.controller;
|
||||||
|
|
||||||
|
import com.inmind.pojo.Address;
|
||||||
|
import com.inmind.pojo.Result;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//@ResponseBody作用于类上,类中每个方法的返回值都会有响应数据的效果
|
||||||
|
@RestController
|
||||||
|
public class ResponseController {
|
||||||
|
/*
|
||||||
|
@RequestMapping("/hello")
|
||||||
|
public String hello(){
|
||||||
|
System.out.println("helloworld");
|
||||||
|
return "helloworld";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/getAddr")
|
||||||
|
public Address getAddr(){
|
||||||
|
Address address = new Address();
|
||||||
|
address.setProvince("江苏");
|
||||||
|
address.setCity("常州");
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/listAddr")
|
||||||
|
public List<Address> listAddr(){
|
||||||
|
Address address = new Address();
|
||||||
|
address.setProvince("江苏");
|
||||||
|
address.setCity("常州");
|
||||||
|
|
||||||
|
Address address1 = new Address();
|
||||||
|
address1.setProvince("江苏");
|
||||||
|
address1.setCity("无锡");
|
||||||
|
ArrayList<Address> list = new ArrayList<>();
|
||||||
|
list.add(address1);
|
||||||
|
list.add(address);
|
||||||
|
return list;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("/hello")
|
||||||
|
public Result hello(){
|
||||||
|
System.out.println("helloworld");
|
||||||
|
return Result.success("hellworld");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/getAddr")
|
||||||
|
public Result getAddr(){
|
||||||
|
Address address = new Address();
|
||||||
|
address.setProvince("江苏");
|
||||||
|
address.setCity("常州");
|
||||||
|
return Result.success(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/listAddr")
|
||||||
|
public Result listAddr(){
|
||||||
|
Address address = new Address();
|
||||||
|
address.setProvince("江苏");
|
||||||
|
address.setCity("常州");
|
||||||
|
|
||||||
|
Address address1 = new Address();
|
||||||
|
address1.setProvince("江苏");
|
||||||
|
address1.setCity("无锡");
|
||||||
|
ArrayList<Address> list = new ArrayList<>();
|
||||||
|
list.add(address1);
|
||||||
|
list.add(address);
|
||||||
|
return Result.success("查询成功", list);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,69 @@
|
|||||||
|
package com.inmind.pojo;
|
||||||
|
//统一响应结果封装类
|
||||||
|
public class Result {
|
||||||
|
private Integer code; //1:成功, 0:失败
|
||||||
|
private String msg;//成功或失败提示信息
|
||||||
|
private Object data;
|
||||||
|
|
||||||
|
public Result() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result(Integer code, String msg, Object data) {
|
||||||
|
this.code = code;
|
||||||
|
this.msg = msg;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义固定的工具类方法
|
||||||
|
public static Result success(String msg,Object data){
|
||||||
|
return new Result(1, msg, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result success(Object data){
|
||||||
|
// return new Result(1, "成功", data);
|
||||||
|
return success("成功", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result success(){
|
||||||
|
// return new Result(1, "成功", null);
|
||||||
|
return success(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result error(){
|
||||||
|
return new Result(0, "错误", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Result{" +
|
||||||
|
"code=" + code +
|
||||||
|
", msg='" + msg + '\'' +
|
||||||
|
", data=" + data +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(Integer code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMsg(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(Object data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user