、概述
二、HTTP请求协议
三、HTTP响应协议
四、请求数据
1.简单实体参数
@RequestMapping("/simpleParam")public String simpleParam(@RequestParam(name ="name" ,required = false ) String username, Integer age){System.out.println (username +":"+age);//自动转换数据类型return "OK";}
2.实体参数
@RequestMapping("/simplePojo")
public String simplePojo(User user){System.out.println ( user );return "ok";
3.数组参数
@RequestMapping("/arrayParam")
public String arrayParam(String [] hobby){System.out.println ( Arrays.toString ( hobby ) );return "ok";
4.集合参数
@RequestMapping("/listParam")public String listParam(@RequestParam List<String> hobby){System.out.println ( hobby );return "ok";}
5.日期参数
@RequestMapping("/dateParam")public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){System.out.println (updateTime);return "ok";}
6.Json参数
@RequestMapping("/jsonParam")public String jsonParam(@RequestBody User user){System.out.println (user);return "ok";}
7.路径参数
//路径参数@RequestMapping("/path/{id}")public String pathParam(@PathVariable Integer id){System.out.println (id);return "ok";}@RequestMapping("/path/{id}/{name}")public String pathParams(@PathVariable Integer id,@PathVariable String name){System.out.println (id);return "ok";}
五、响应数据
package com.itheima.controller;import com.itheima.pojo.Address;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
public class ResponseController {@RequestMapping("hello")public String hello(){System.out.println ("Hello World ~");return "Hello World";}@RequestMapping("/getAddr")public Address getAddr(){Address addr=new Address ();addr.setProvince ( "广东" );addr.setCity ( "深圳" );return addr;}@RequestMapping("/listAddr")public List<Address> listAddr(){List<Address> list =new ArrayList<> ();Address addr=new Address ();addr.setProvince ( "广东" );addr.setCity ( "西安" );Address addr2=new Address ();addr2.setProvince ( "陕西" );addr2.setCity ( "西安" );list.add ( addr );list.add ( addr2 );return list;}
}
package com.itheima.pojo;/*** 统一响应结果封装类*/
public class Result {private Integer code ;//1 成功 , 0 失败private String msg; //提示信息private Object data; //数据 datapublic Result() {}public Result(Integer code, String msg, Object data) {this.code = code;this.msg = msg;this.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;}public static Result success(Object data){return new Result(1, "success", data);}public static Result success(){return new Result(1, "success", null);}public static Result error(String msg){return new Result(0, msg, null);}@Overridepublic String toString() {return "Result{" +"code=" + code +", msg='" + msg + '\'' +", data=" + data +'}';}
}
import com.itheima.pojo.Address;
import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
public class ResponseController {@RequestMapping("hello")public Result hello(){System.out.println ("Hello World ~");// return new Result (1,"success","Hello World");return Result.success ("hello world");}@RequestMapping("/getAddr")public Result getAddr(){Address addr=new Address ();addr.setProvince ( "广东" );addr.setCity ( "深圳" );return Result.success (addr);}@RequestMapping("/listAddr")public Result listAddr(){List<Address> list =new ArrayList<> ();Address addr=new Address ();addr.setProvince ( "广东" );addr.setCity ( "西安" );Address addr2=new Address ();addr2.setProvince ( "陕西" );addr2.setCity ( "西安" );list.add ( addr );list.add ( addr2 );return Result.success (list);}
}
六、请求响应---响应案例
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class EmpController {
@RequestMapping("/listEmp")public Result list(){//1.加载并解析xml文件String file=this.getClass ().getClassLoader ().getResource ( "emp.xml" ).getFile ();System.out.println ( file );List<Emp> empList = XmlParserUtils.parse ( file, Emp.class );//2.对数据进行转换处理empList.stream ().forEach (emp -> {//处理gender 1:男 2:女String gender = emp.getGender ();if ("1".equals (gender)){emp.setGender ( "男" );}else if("2".equals (gender)){emp.setGender ( "女" );}String job = emp.getJob ();if ("1".equals ( job )){emp.setJob ( "讲师" );}else if ("2".equals ( "gender" )){emp.setJob ( "班主任" );}else if ("3".equals ( job )){emp.setJob ( "就业指导" );}} );//3.响应数据return Result.success (empList);}
}
七、分层解耦