package com.wjh.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/*
* 测试请求参数接受
*/
@RestController
public class RequestController {
// //原始方式
// @RequestMapping ("/simpleParam")
// public String simpleParam(HttpServletRequest request){
// //获取请求参数
// String name = request.getParameter("name");
// String ageStr = request.getParameter("age");
//
// int age = Integer.parseInt(ageStr);
// System.out.println(name + ":" + age + ":");
//
// return "OK!";
// }
//springboot 方式
@RequestMapping ("/simpleParam")
public String simpleParam( String name,Integer age){
System.out.println(name + ":" + age + ":");
return "OK!";
}
}
GET请求:
POST请求:
package com.wjh.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/*
* 测试请求参数接受
*/
@RestController
public class RequestController {
// //原始方式
// @RequestMapping ("/simpleParam")
// public String simpleParam(HttpServletRequest request){
// //获取请求参数
// String name = request.getParameter("name");
// String ageStr = request.getParameter("age");
//
// int age = Integer.parseInt(ageStr);
// System.out.println(name + ":" + age + ":");
//
// return "OK!";
// }
//springboot 方式
// @RequestMapping ("/simpleParam")
// public String simpleParam( String name,Integer age){
// System.out.println(name + ":" + age + ":");
// return "OK!";
// }
@RequestMapping ("/simpleParam")
public String simpleParam(@RequestParam(name = "name") String username, Integer age){
System.out.println(username + ":" + age + ":");
return "OK!";
}
}
创建一个User类:
package com.wjh.pojo;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建一个Address类
package com.wjh.pojo;
public class Address {
private String province; //省
private String city; //市
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
package com.wjh.controller;
import com.wjh.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/*
* 测试请求参数接受
*/
@RestController
public class RequestController {
// //原始方式
// @RequestMapping ("/simpleParam")
// public String simpleParam(HttpServletRequest request){
// //获取请求参数
// String name = request.getParameter("name");
// String ageStr = request.getParameter("age");
//
// int age = Integer.parseInt(ageStr);
// System.out.println(name + ":" + age + ":");
//
// return "OK!";
// }
//springboot 方式
// @RequestMapping ("/simpleParam")
// public String simpleParam( String name,Integer age){
// System.out.println(name + ":" + age + ":");
// return "OK!";
// }
// @RequestMapping ("/simpleParam")
// public String simpleParam(@RequestParam(name = "name", required = false) String username, Integer age){
// System.out.println(username + ":" + age + ":");
// return "OK!";
// }
//2.实体参http://localhost:8080/simpleParam数
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
System.out.println("姓名:" + user.getName());
System.out.println("年龄:" + user.getAge());
System.out.println("省份:" + user.getAddress().getProvince());
System.out.println("城市:" + user.getAddress().getCity());
return "YES OK!";
}
}
//3.数组集合参数
@RequestMapping("/arrayParam")
public String arrayParam(String[] hobby){
System.out.println(hobby);
System.out.println(Arrays.toString(hobby));
return "YES OK!";
}
//集合参数
@RequestMapping("/listParam")
public String listParam(@RequestParam List hobby){
System.out.println(hobby);
return "YES OK!";
}
//6.json参数
@RequestMapping("/jsonParam")
//@DateTimeFormat -> 指定时间传递的参数格式
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "YES OK!";
}
{
"name": "小黑子",
"age": 18,
"address": {
"province": "江西",
"city": "南昌"
}
}
package com.wjh.controller;
import com.wjh.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
/*
* 测试请求参数接受
*/
@RestController
public class RequestController {
// //原始方式
// @RequestMapping ("/simpleParam")
// public String simpleParam(HttpServletRequest request){
// //获取请求参数
// String name = request.getParameter("name");
// String ageStr = request.getParameter("age");
//
// int age = Integer.parseInt(ageStr);
// System.out.println(name + ":" + age + ":");
//
// return "OK!";
// }
//springboot 方式
@RequestMapping ("/simpleParam")
public String simpleParam( String name,Integer age){
System.out.println(name + ":" + age + ":");
return "OK!";
}
@RequestMapping ("/simpleParamF")
public String simpleParamF(@RequestParam(name = "name", required = false) String username, Integer age){
System.out.println(username + ":" + age + ":");
return "OK!";
}
//2.实体参http://localhost:8080/simpleParam数
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
System.out.println("姓名:" + user.getName());
System.out.println("年龄:" + user.getAge());
System.out.println("省份:" + user.getAddress().getProvince());
System.out.println("城市:" + user.getAddress().getCity());
return "YES OK!";
}
//3.数组参数
@RequestMapping("/arrayParam")
public String arrayParam(String[] hobby){
System.out.println(hobby);
System.out.println(Arrays.toString(hobby));
return "YES OK!";
}
//4.集合参数
@RequestMapping("/listParam")
public String listParam(@RequestParam List hobby){
System.out.println(hobby);
return "YES OK!";
}
//5.日期时间参数
//http://localhost:8080/dateParam?updateTime=2023-09-18 09:12:30
@RequestMapping("/dateParam")
//@DateTimeFormat -> 指定时间传递的参数格式
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "YES OK!";
}
//6.json参数
@RequestMapping("/jsonParam")
//@DateTimeFormat -> 指定时间传递的参数格式
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "YES OK!";
}
//7.路径参数
@RequestMapping("/path/{id}")
//@DateTimeFormat -> 指定时间传递的参数格式
public String pathParam(@PathVariable Integer id){
System.out.println(id);
return "YES OK!";
}
//8.路径参数
//@DateTimeFormat -> 指定时间传递的参数格式
@RequestMapping("/path/{id}/{name}")
public String pathParam2(@PathVariable Integer id, @PathVariable String name){
System.out.println(id);
System.out.println(name);
return "YES OK!";
}
}
package com.wjh.controller;
import com.wjh.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 word!");
return "Hello word!";
}
@RequestMapping("/getAddress")
public Address getAddress(){
Address address = new Address();
address.setProvince("江西");
address.setCity("南昌");
System.out.println(address.getProvince() + "\n" + address.getCity());
return address;
}
@RequestMapping("/listAddress")
public List listAddress(){
List list = new ArrayList<>();
Address address = new Address();
address.setProvince("江西");
address.setCity("南昌");
Address address2 = new Address();
address2.setProvince("广东");
address2.setCity("深圳");
list.add(address);
list.add(address2);
for (Address lists: list) {
System.out.println(lists);
}
return list;
}
}
package com.wjh.pojo;
/**
* 统一响应结果封装类
*/
public class Result {
private Integer code ;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 data
public 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);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
结果是一样的,提高了实用性
Springboot项目的静态资源(html,css,js等前端资源)默认存放目录为:
classpath:/static 、 classpath:/public、 classpath:/resources
package com.wjh.utils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class XmlParserUtils {
public static List parse(String file , Class targetClass) {
ArrayList list = new ArrayList(); //封装解析出来的数据
try {
//1.获取一个解析器对象
SAXReader saxReader = new SAXReader();
//2.利用解析器把xml文件加载到内存中,并返回一个文档对象
Document document = saxReader.read(new File(file));
//3.获取到根标签
Element rootElement = document.getRootElement();
//4.通过根标签来获取 user 标签
List elements = rootElement.elements("emp");
//5.遍历集合,得到每一个 user 标签
for (Element element : elements) {
//获取 name 属性
String name = element.element("name").getText();
//获取 age 属性
String age = element.element("age").getText();
//获取 image 属性
String image = element.element("image").getText();
//获取 gender 属性
String gender = element.element("gender").getText();
//获取 job 属性
String job = element.element("job").getText();
//组装数据
Constructor constructor = targetClass.getDeclaredConstructor(String.class, Integer.class, String.class, String.class, String.class);
constructor.setAccessible(true);
T object = constructor.newInstance(name, Integer.parseInt(age), image, gender, job);
list.add(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
package com.wjh.pojo;
public class Emp {
private String name;
private Integer age;
private String image;
private String gender;
private String job;
public Emp() {
}
public Emp(String name, Integer age, String image, String gender, String job) {
this.name = name;
this.age = age;
this.image = image;
this.gender = gender;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", age=" + age +
", image='" + image + '\'' +
", gender='" + gender + '\'' +
", job='" + job + '\'' +
'}';
}
}
package com.wjh.controller;
import com.wjh.pojo.Emp;
import com.wjh.pojo.Result;
import com.wjh.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmoController {
@RequestMapping("/listEmp")
public Result list(){
//1.加载并解析emp.xml文件
//String file = "D:\\JavaWeb2023\\java_web_project\\springboot-web-req-resp\\src\\main\\resources\\emp.xml";
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
System.out.println(file);
List empList = XmlParserUtils.parse(file, Emp.class);
//2.对数据进行转换处理 -gender, job
empList.stream().forEach(emp -> {
//处理gander 1: 男, 2: 女
String gander = emp.getGender();
if("1".equals(gander)){
emp.setGender("男");
}else if("2".equals(gander)){
emp.setGender("女");
}
//处理job - 1:讲师, 2:班主任, 3:就业指导
String job = emp.getJob();
if("1".equals(job)){
emp.setJob("讲师");
}else if("2".equals(job)){
emp.setJob("班主任");
}else if("3".equals(job)){
emp.setJob("就业指导");
}
});
//3.响应数据
return Result.success(empList);
}
}
package com.wjh.controller;
import com.wjh.pojo.Emp;
import com.wjh.pojo.Result;
import com.wjh.service.EmpService;
import com.wjh.service.impl.EmpServiceA;
import com.wjh.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmoController {
private EmpService empService = new EmpServiceA();
@RequestMapping("/listEmp")
public Result list() {
//1.调用service,获取数据
List empList = empService.listEmp();
//3.响应数据
return Result.success(empList);
}
}
// //1.加载并解析emp.xml文件
//
// //String file = "D:\\JavaWeb2023\\java_web_project\\springboot-web-req-resp\\src\\main\\resources\\emp.xml";
// String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
// System.out.println(file);
// List empList = XmlParserUtils.parse(file, Emp.class);
//
// //2.对数据进行转换处理 -gender, job
// empList.stream().forEach(emp -> {
// //处理gander 1: 男, 2: 女
// String gander = emp.getGender();
// if("1".equals(gander)){
// emp.setGender("男");
// }else if("2".equals(gander)){
// emp.setGender("女");
// }
//
// //处理job - 1:讲师, 2:班主任, 3:就业指导
// String job = emp.getJob();
// if("1".equals(job)){
// emp.setJob("讲师");
// }else if("2".equals(job)){
// emp.setJob("班主任");
// }else if("3".equals(job)){
// emp.setJob("就业指导");
// }
//
// });
package com.wjh.Dao.impl;
import com.wjh.Dao.EmpDao;
import com.wjh.pojo.Emp;
import com.wjh.utils.XmlParserUtils;
import java.util.List;
public class EmoDaoA implements EmpDao {
@Override
public List listEmp() {
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
System.out.println(file);
List empList = XmlParserUtils.parse(file, Emp.class);
return empList;
}
}
package com.wjh.Dao;
import com.wjh.pojo.Emp;
import java.util.List;
public interface EmpDao {
//获取员工列表数据
public List listEmp();
}
package com.wjh.pojo;
public class Emp {
private String name;
private Integer age;
private String image;
private String gender;
private String job;
public Emp() {
}
public Emp(String name, Integer age, String image, String gender, String job) {
this.name = name;
this.age = age;
this.image = image;
this.gender = gender;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", age=" + age +
", image='" + image + '\'' +
", gender='" + gender + '\'' +
", job='" + job + '\'' +
'}';
}
}
package com.wjh.service.impl;
import com.wjh.Dao.EmpDao;
import com.wjh.Dao.impl.EmoDaoA;
import com.wjh.pojo.Emp;
import com.wjh.service.EmpService;
import java.util.List;
public class EmpServiceA implements EmpService {
private EmpDao empDao = new EmoDaoA();
@Override
public List listEmp() {
//1.调用dao,获取数据
List empList = empDao.listEmp();
//2.对数据进行转换处理 -gender, job
empList.stream().forEach(emp -> {
//处理gander 1: 男, 2: 女
String gander = emp.getGender();
if("1".equals(gander)){
emp.setGender("男");
}else if("2".equals(gander)){
emp.setGender("女");
}
//处理job - 1:讲师, 2:班主任, 3:就业指导
String job = emp.getJob();
if("1".equals(job)){
emp.setJob("讲师");
}else if("2".equals(job)){
emp.setJob("班主任");
}else if("3".equals(job)){
emp.setJob("就业指导");
}
});
return empList;
}
}
package com.wjh.service;
import com.wjh.pojo.Emp;
import java.util.List;
public interface EmpService {
//获取员工列表
public List listEmp();
}
控制反转:
Inversion Of Control,简称IOC。对象的创建控制权由程序自身转移到外部(容器),这种思想称为控制反转。
依赖注入:Dependency Injection,简称DI。容器为应用程序提供运行时,所依赖的资源,称之为依赖注入。
Bean对象:IOC容器中创建、管理的对象,称之为bean。
@Component //将当前类交给IOC容器管理,成为IOC容器中的bean
@Autowired //运行时,IOC容器会提供该类型的bean对象,并复制给改变量 - 依赖注入
@Primary --> 在又多个相同bean情况下,想用的实现方法上加这个注解
@Qualifier --> @Qualifier("A") 或者@Qualifier ("B")
@Resource --> 不属于springboot,属于JDK当中的,加在private EmpDao empDao 上