From 86cafedf57b3657f87db0249a612ff91c0b86ab7 Mon Sep 17 00:00:00 2001 From: peri <2698953178@qq.com> Date: Thu, 19 Sep 2024 23:31:28 +0800 Subject: [PATCH] Controller --- .../05_SpringMVC_Quick_Start_Web_exploded.xml | 13 ++++++ .idea/compiler.xml | 16 +++++-- .idea/encodings.xml | 17 +++++++ .idea/jarRepositories.xml | 5 ++ .idea/misc.xml | 10 ++-- 02-Spring-Dependency-Injection-1/pom.xml | 9 +++- .../src/main/java/edu/leijiaqi/Main.java | 4 +- .../java/edu/leijiaqi/entity/ArtTeacher.java | 3 +- .../java/edu/leijiaqi/entity/PETeacher.java | 2 +- .../java/edu/leijiaqi/entity/Student.java | 16 +++++-- .../src/main/resources/application.xml | 6 ++- 03-Spring-Annotate/pom.xml | 17 +++++++ .../main/java/edu/leijiaqi/ArtTeacher.java | 7 +++ .../src/main/java/edu/leijiaqi/Main.java | 11 +++++ .../src/main/java/edu/leijiaqi/PETeacher.java | 7 +++ .../src/main/java/edu/leijiaqi/Student.java | 11 +++++ .../src/main/java/edu/leijiaqi/Teacher.java | 4 ++ .../leijiaqi/config/MainConfiguration.java | 9 ++++ 04-Spring-Annotate/pom.xml | 17 +++++++ .../leijiaqi/controller/IndexController.java | 15 ++++++ .../src/main/resources/springmvc.xml | 33 +++++++++++++ .../src/main/webapp/web/WEB-INF/web.xml | 6 +++ .../Controller/ArrayParamsController.java | 25 ++++++++++ .../Controller/DateParamsController.java | 18 ++++++++ .../Controller/HelloController.java | 25 ++++++++++ .../Controller/JsonParamsController.java | 17 +++++++ .../Controller/PathParamsController.java | 33 +++++++++++++ .../Controller/PojoParamsController.java | 21 +++++++++ .../Controller/SimpleParamsController.java | 46 +++++++++++++++++++ .../springbootquickstart/pojo/Address.java | 10 ++++ .../springbootquickstart/pojo/User.java | 18 ++++++++ JavaEE-2024.iml | 1 + 32 files changed, 431 insertions(+), 21 deletions(-) create mode 100644 .idea/artifacts/05_SpringMVC_Quick_Start_Web_exploded.xml create mode 100644 .idea/encodings.xml create mode 100644 03-Spring-Annotate/pom.xml create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/ArtTeacher.java create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/Main.java create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/PETeacher.java create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/Student.java create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/Teacher.java create mode 100644 03-Spring-Annotate/src/main/java/edu/leijiaqi/config/MainConfiguration.java create mode 100644 04-Spring-Annotate/pom.xml create mode 100644 05-SpringMVC-Quick-Start/src/main/java/edu/leijiaqi/controller/IndexController.java create mode 100644 05-SpringMVC-Quick-Start/src/main/resources/springmvc.xml create mode 100644 05-SpringMVC-Quick-Start/src/main/webapp/web/WEB-INF/web.xml create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/ArrayParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/DateParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/HelloController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/JsonParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PathParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PojoParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/SimpleParamsController.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/Address.java create mode 100644 06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/User.java diff --git a/.idea/artifacts/05_SpringMVC_Quick_Start_Web_exploded.xml b/.idea/artifacts/05_SpringMVC_Quick_Start_Web_exploded.xml new file mode 100644 index 0000000..6a8cbe8 --- /dev/null +++ b/.idea/artifacts/05_SpringMVC_Quick_Start_Web_exploded.xml @@ -0,0 +1,13 @@ + + + $PROJECT_DIR$/out/artifacts/05_SpringMVC_Quick_Start_Web_exploded + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 2c5b1dd..64364ac 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,15 +2,23 @@ + - + + + + + + - - - + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..69b0f96 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index 712ab9d..c7ea920 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -16,5 +16,10 @@ - diff --git a/02-Spring-Dependency-Injection-1/pom.xml b/02-Spring-Dependency-Injection-1/pom.xml index 8ef5dbd..8cfb487 100644 --- a/02-Spring-Dependency-Injection-1/pom.xml +++ b/02-Spring-Dependency-Injection-1/pom.xml @@ -5,7 +5,7 @@ 4.0.0 edu.leijiaqi - 01-Spring-Quick-Start + 02-Spring-Dependency-Injection-1 1.0-SNAPSHOT @@ -19,6 +19,11 @@ spring-context 6.1.11 + + org.projectlombok + lombok + 1.18.26 + - + \ No newline at end of file diff --git a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/Main.java b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/Main.java index f8e5d68..823729d 100644 --- a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/Main.java +++ b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/Main.java @@ -1,6 +1,7 @@ package edu.leijiaqi; import edu.leijiaqi.entity.Student; + import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -9,7 +10,6 @@ public class Main { ApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); Student student = (Student) context.getBean("student"); student.study(); + System.out.println(student); } } - - diff --git a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/ArtTeacher.java b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/ArtTeacher.java index 6517dbe..30111e9 100644 --- a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/ArtTeacher.java +++ b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/ArtTeacher.java @@ -1,7 +1,6 @@ package edu.leijiaqi.entity; -public class ArtTeacher implements Teacher{ - @Override +public class ArtTeacher { public void teach() { System.out.println("我是美术老师,我教你画画"); } diff --git a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/PETeacher.java b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/PETeacher.java index c0904f1..d7c6c25 100644 --- a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/PETeacher.java +++ b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/PETeacher.java @@ -1,6 +1,6 @@ package edu.leijiaqi.entity; -public class PETeacher implements Teacher{ +public class PETeacher implements Teacher { public void teach() { System.out.println("我是体育老师,我教你运动"); } diff --git a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/Student.java b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/Student.java index ecbd438..41bfd97 100644 --- a/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/Student.java +++ b/02-Spring-Dependency-Injection-1/src/main/java/edu/leijiaqi/entity/Student.java @@ -1,12 +1,22 @@ package edu.leijiaqi.entity; +import lombok.Data; + +@Data public class Student { - private Teacher teacher = new PETeacher(); + + // private Teacher teacher = new PETeacher(); + private Teacher teacher; + private String name; public void study(){ teacher.teach(); } - // 要使用依赖注入 要提供一个set方法 - public void setTeacher(Teacher teacher){ + public Student(Teacher teacher, String name){ this.teacher = teacher; + this.name = name; } + // 要使用依赖注入 要提供一个set方法 + // public void setTeacher(Teacher teacher){ + // this.teacher = teacher; + // } } diff --git a/02-Spring-Dependency-Injection-1/src/main/resources/application.xml b/02-Spring-Dependency-Injection-1/src/main/resources/application.xml index e1d52fc..15fd616 100644 --- a/02-Spring-Dependency-Injection-1/src/main/resources/application.xml +++ b/02-Spring-Dependency-Injection-1/src/main/resources/application.xml @@ -2,8 +2,10 @@ - + - + + + \ No newline at end of file diff --git a/03-Spring-Annotate/pom.xml b/03-Spring-Annotate/pom.xml new file mode 100644 index 0000000..54a057d --- /dev/null +++ b/03-Spring-Annotate/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + edu.leijiaqi + 03-Spring-Annotate + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + \ No newline at end of file diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/ArtTeacher.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/ArtTeacher.java new file mode 100644 index 0000000..1efedf4 --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/ArtTeacher.java @@ -0,0 +1,7 @@ +package edu.leijiaqi; + +public class ArtTeacher { + public void teach() { + System.out.println("美术"); + } +} diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/Main.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Main.java new file mode 100644 index 0000000..6fe18b5 --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Main.java @@ -0,0 +1,11 @@ +package edu.leijiaqi; + +import edu.leijiaqi.config.MainConfiguration; + +public class Main { + public static void main(String[] args) { + ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class); + Student student =(Student)context.getBean(); + + } +} diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/PETeacher.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/PETeacher.java new file mode 100644 index 0000000..3aada56 --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/PETeacher.java @@ -0,0 +1,7 @@ +package edu.leijiaqi; + +public class PETeacher { + public void teach() { + System.out.println("体育"); + } +} diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/Student.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Student.java new file mode 100644 index 0000000..449b6b4 --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Student.java @@ -0,0 +1,11 @@ +package edu.leijiaqi; + +public class Student { + private Teacher teacher; + public Student(Teacher teacher) { + this.teacher = teacher; + } + public void study() { + teacher.teach(); + } +} diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/Teacher.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Teacher.java new file mode 100644 index 0000000..b36eec7 --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/Teacher.java @@ -0,0 +1,4 @@ +package edu.leijiaqi; + +public interface Teacher { +} diff --git a/03-Spring-Annotate/src/main/java/edu/leijiaqi/config/MainConfiguration.java b/03-Spring-Annotate/src/main/java/edu/leijiaqi/config/MainConfiguration.java new file mode 100644 index 0000000..bc2c27c --- /dev/null +++ b/03-Spring-Annotate/src/main/java/edu/leijiaqi/config/MainConfiguration.java @@ -0,0 +1,9 @@ +package edu.leijiaqi.config; + +import edu.leijiaqi.Teacher; + +public class MainConfiguration implements Teacher { + private Teacher teacher; + public Student() + +} diff --git a/04-Spring-Annotate/pom.xml b/04-Spring-Annotate/pom.xml new file mode 100644 index 0000000..236f66f --- /dev/null +++ b/04-Spring-Annotate/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + edu.leijiaqi + 04-Spring-Annotate + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + \ No newline at end of file diff --git a/05-SpringMVC-Quick-Start/src/main/java/edu/leijiaqi/controller/IndexController.java b/05-SpringMVC-Quick-Start/src/main/java/edu/leijiaqi/controller/IndexController.java new file mode 100644 index 0000000..e20cea6 --- /dev/null +++ b/05-SpringMVC-Quick-Start/src/main/java/edu/leijiaqi/controller/IndexController.java @@ -0,0 +1,15 @@ +package edu.leijiaqi.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class IndexController { + @RequestMapping("/hello") + @ResponseBody + public String hello(){ + System.out.println("Welcome to Spring MVC"); + return "Hello Spring MVC"; + } +} diff --git a/05-SpringMVC-Quick-Start/src/main/resources/springmvc.xml b/05-SpringMVC-Quick-Start/src/main/resources/springmvc.xml new file mode 100644 index 0000000..c31cc50 --- /dev/null +++ b/05-SpringMVC-Quick-Start/src/main/resources/springmvc.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/05-SpringMVC-Quick-Start/src/main/webapp/web/WEB-INF/web.xml b/05-SpringMVC-Quick-Start/src/main/webapp/web/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/05-SpringMVC-Quick-Start/src/main/webapp/web/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/ArrayParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/ArrayParamsController.java new file mode 100644 index 0000000..c01d910 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/ArrayParamsController.java @@ -0,0 +1,25 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +public class ArrayParamsController { + //数组参数 + @RequestMapping(value = "/arrayTest",method = RequestMethod.POST) + public String arrayTest(String[] hobby){ + System.out.println(Arrays.toString(hobby)); + return "Get 请求,hobby=" + Arrays.toString(hobby); + } + //集合参数 + @RequestMapping(value = "arrayTest1",method = RequestMethod.POST) + public String arrayTest1(@RequestParam List hobby) { + System.out.println(hobby); + return "Get 请求,hobby=" + hobby; + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/DateParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/DateParamsController.java new file mode 100644 index 0000000..0c66c50 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/DateParamsController.java @@ -0,0 +1,18 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; + +@RestController +public class DateParamsController { + //URL:http://localhost:8080/dateTest?dateTime=2024-09-19 16:52:00 + @RequestMapping(value = "/dateTest") + public String dateTest(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime dateTime){ + System.out.println(dateTime); + return dateTime.toString(); + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/HelloController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/HelloController.java new file mode 100644 index 0000000..8a5ebca --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/HelloController.java @@ -0,0 +1,25 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +//注解 +@RestController + +public class HelloController { + + //URL:协议://IP:端口/路径 + //URL:http://localhost:8080/hello + //注解 +// @GetMapping("/hello") //通过Get请求访问到方法 +// public String hello() { +// return "你好。。。。。。。"; +// } + @RequestMapping(value = "/hello",method = RequestMethod.GET) + public String hello(){ + return "Hello World..."; + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/JsonParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/JsonParamsController.java new file mode 100644 index 0000000..f97af16 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/JsonParamsController.java @@ -0,0 +1,17 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import edu.leijiaqi.springbootquickstart.pojo.User; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class JsonParamsController { + + @RequestMapping(value = "/jsonTest1",method = RequestMethod.POST) + public String jsonTest1(@RequestBody User user){ + System.out.println(user); + return "Post 请求,name="+user.getUsername()+",password="+user.getPassword()+",province="+user.getAddress().getProvince()+",city="+user.getAddress().getCity(); + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PathParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PathParamsController.java new file mode 100644 index 0000000..793602e --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PathParamsController.java @@ -0,0 +1,33 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PathParamsController { + @RequestMapping(value = "/pathTest1/{id}",method = RequestMethod.POST) + public String pathTest1(@PathVariable Integer id) { + return "Path:id="+id; + } + + @RequestMapping(value = "/pathTest2/{id}/{name}",method = RequestMethod.POST) + public String pathTest2(@PathVariable Integer id,@PathVariable String name) { + return "Path:id="+id+",name="+name; + } + + //通配符请求 + + //**任意路径 + @RequestMapping(value = "/pathTest3/**",method = RequestMethod.POST) + public String pathTest3(){ + return "通配符请求:**"; + } + + //*只能有一级路径 + @RequestMapping(value = "/pathTest4/*",method = RequestMethod.POST) + public String pathTest4(){ + return "通配符请求:*"; + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PojoParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PojoParamsController.java new file mode 100644 index 0000000..c55f3e8 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/PojoParamsController.java @@ -0,0 +1,21 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import edu.leijiaqi.springbootquickstart.pojo.User; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PojoParamsController { + @RequestMapping(value = "/pojoTest1",method = RequestMethod.POST) + public String pojoTest1(User user){ + System.out.println(user); + return "Post 请求,name="+user.getUsername()+",password="+user.getPassword(); + } + + @RequestMapping(value = "pojoTest2",method = RequestMethod.POST) + public String pojoTest2(User user){ + System.out.println(user); + return "Post 请求,name="+user.getUsername()+",password="+user.getPassword()+",province="+user.getAddress().getProvince()+",city="+user.getAddress().getCity(); + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/SimpleParamsController.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/SimpleParamsController.java new file mode 100644 index 0000000..a749173 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/Controller/SimpleParamsController.java @@ -0,0 +1,46 @@ +package edu.leijiaqi.springbootquickstart.Controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController + +public class SimpleParamsController { + //1. Get 无参数 + @RequestMapping(value = "/getTest1",method = RequestMethod.GET) + public String getTest1(){ + return "Get 请求"; + } + + //2. Get 有参数 + //URL:http://localhost:8080/getTest2?nickname=leijiaqi&age=20 + @RequestMapping(value = "/getTest2",method = RequestMethod.GET) + public String getTest2(String nickname,int age){ + System.out.println("Get 请求,name="+nickname+",age="+age); + return "Get 请求,name="+nickname+",age="+age; + } + + //3. Get 有参数 方法的参数和请求的参数不一致 + //URL:http://localhost:8080/getTest3?nickname=leijiaqi&age=20 + @RequestMapping(value = "/getTest3",method = RequestMethod.GET) + public String getTest3(@RequestParam(value = "nickname",required = false) String nickname, int age){ + System.out.println("Get 请求,name="+nickname+",age="+age); + return "Get 请求,name="+nickname+",age="+age; + } + + //4.Post 无参数 + @RequestMapping(value = "postTest1",method = RequestMethod.POST) + public String postTest1(){ + return "Post 请求"; + } + + //5.Post 有参数 + //URL:http://localhost:8080/postTest2 + @RequestMapping(value = "postTest2",method = RequestMethod.POST) + public String postTest2(String nickname,int age){ + System.out.println("Post 请求,name="+nickname+",age="+age); + return "Post 请求,name="+nickname+",age="+age; + } +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/Address.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/Address.java new file mode 100644 index 0000000..d9190bc --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/Address.java @@ -0,0 +1,10 @@ +package edu.leijiaqi.springbootquickstart.pojo; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Address { + private String province; + private String city; +} diff --git a/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/User.java b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/User.java new file mode 100644 index 0000000..cb40ca5 --- /dev/null +++ b/06-SpringBoot-Quick-Start/src/main/java/edu/leijiaqi/springbootquickstart/pojo/User.java @@ -0,0 +1,18 @@ +package edu.leijiaqi.springbootquickstart.pojo; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class User { + private String username; + private String password; + private Address address; + + //右键+generate+getter and setter + + public String toString() { + return "User{" + "username='" + username + '\'' + ", password=' " + password + '\'' + '}'; + } +} diff --git a/JavaEE-2024.iml b/JavaEE-2024.iml index 9a5cfce..4ebfb83 100644 --- a/JavaEE-2024.iml +++ b/JavaEE-2024.iml @@ -3,6 +3,7 @@ + \ No newline at end of file