Browse Source

0518 请求和响应

Qing 1 year ago
parent
commit
1bf5882aff

+ 47 - 40
springmvc-demo/.idea/workspace.xml

@@ -10,30 +10,17 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="0515 springmvc">
+    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="0518 整合mybatis">
       <change afterPath="$PROJECT_DIR$/../.idea/VIP31.iml" afterDir="false" />
       <change afterPath="$PROJECT_DIR$/../JavaSE/JavaSe01/JavaSe01.iml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/artifacts/springmvc_demo_Web_exploded.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/dao/BookDao.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/Book.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/service/BookService.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/service/impl/BookServiceImpl.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/db.properties" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/mapper/BookMapper.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/mybatis-config.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/spring.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/TestMybatis.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/TestSpringMvc.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/HeaderController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/JsonController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/ParamController.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/MyServletContainerInitializer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/MyServletContainerInitializer.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/webapp/hello.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/hello.jsp" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -57,6 +44,10 @@
   <component name="Git.Settings">
     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
   </component>
+  <component name="HttpClientSelectedEnvironments">
+    <file url="jar://$APPLICATION_HOME_DIR$/plugins/restClient/lib/restClient.jar!/com/intellij/ws/rest/client/requests/collection/post-requests.http" environment="test" />
+    <file url="file:///private/var/folders/_5/09sbbdyn12l7q49bq010rm8m0000gn/T/whats-new-dir/whats-new.http" environment="test" />
+  </component>
   <component name="KubernetesApiPersistence">{}</component>
   <component name="KubernetesApiProvider">{
   &quot;isMigrated&quot;: true
@@ -90,6 +81,10 @@
   </component>
   <component name="PropertiesComponent"><![CDATA[{
   "keyToString": {
+    "HTTP Request.rest-api_10 | #1.executor": "Run",
+    "HTTP Request.rest-api_11 | #1.executor": "Run",
+    "HTTP Request.rest-api_8 | #1.executor": "Run",
+    "HTTP Request.rest-api_9 | #1.executor": "Run",
     "JUnit.TestMybatis.testMybatis.executor": "Run",
     "JUnit.TestSpringMvc.test.executor": "Run",
     "Maven. [org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate].executor": "Run",
@@ -118,6 +113,18 @@
     </key>
   </component>
   <component name="RunManager" selected="Tomcat Server.Tomcat 10.0.21">
+    <configuration name="rest-api_10 | #1" type="HttpClient.HttpRequestRunConfigurationType" factoryName="HTTP Request" temporary="true" nameIsGenerated="true" path="$APPLICATION_CONFIG_DIR$/scratches/rest-api_10.http" requestIdentifier="#1" runType="Run single request">
+      <method v="2" />
+    </configuration>
+    <configuration name="rest-api_11 | #1" type="HttpClient.HttpRequestRunConfigurationType" factoryName="HTTP Request" temporary="true" nameIsGenerated="true" path="$APPLICATION_CONFIG_DIR$/scratches/rest-api_11.http" requestIdentifier="#1" runType="Run single request">
+      <method v="2" />
+    </configuration>
+    <configuration name="rest-api_8 | #1" type="HttpClient.HttpRequestRunConfigurationType" factoryName="HTTP Request" temporary="true" nameIsGenerated="true" path="$APPLICATION_CONFIG_DIR$/scratches/rest-api_8.http" requestIdentifier="#1" runType="Run single request">
+      <method v="2" />
+    </configuration>
+    <configuration name="rest-api_9 | #1" type="HttpClient.HttpRequestRunConfigurationType" factoryName="HTTP Request" temporary="true" nameIsGenerated="true" path="$APPLICATION_CONFIG_DIR$/scratches/rest-api_9.http" requestIdentifier="#1" runType="Run single request">
+      <method v="2" />
+    </configuration>
     <configuration name="TestMybatis.testMybatis" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="springmvc-demo" />
       <extension name="coverage">
@@ -134,22 +141,6 @@
         <option name="Make" enabled="true" />
       </method>
     </configuration>
-    <configuration name="TestSpringMvc.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
-      <module name="springmvc-demo" />
-      <extension name="coverage">
-        <pattern>
-          <option name="PATTERN" value="com.sf.*" />
-          <option name="ENABLED" value="true" />
-        </pattern>
-      </extension>
-      <option name="PACKAGE_NAME" value="com.sf" />
-      <option name="MAIN_CLASS_NAME" value="com.sf.TestSpringMvc" />
-      <option name="METHOD_NAME" value="test" />
-      <option name="TEST_OBJECT" value="method" />
-      <method v="2">
-        <option name="Make" enabled="true" />
-      </method>
-    </configuration>
     <configuration default="true" type="JetRunConfigurationType">
       <method v="2">
         <option name="Make" enabled="true" />
@@ -162,7 +153,8 @@
       </method>
     </configuration>
     <configuration name="Tomcat 10.0.21" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 10.0.21" ALTERNATIVE_JRE_ENABLED="false" nameIsGenerated="true">
-      <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/springmvc_demo/hello" />
+      <option name="OPEN_IN_BROWSER" value="false" />
+      <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/springmvc_demo/book/hello" />
       <option name="UPDATING_POLICY" value="restart-server" />
       <deployment>
         <artifact name="springmvc-demo:war">
@@ -256,14 +248,20 @@
       </method>
     </configuration>
     <list>
+      <item itemvalue="HTTP Request.rest-api_10 | #1" />
+      <item itemvalue="HTTP Request.rest-api_11 | #1" />
+      <item itemvalue="HTTP Request.rest-api_8 | #1" />
+      <item itemvalue="HTTP Request.rest-api_9 | #1" />
       <item itemvalue="JUnit.TestMybatis.testMybatis" />
-      <item itemvalue="JUnit.TestSpringMvc.test" />
       <item itemvalue="Tomcat Server.Tomcat 10.0.21" />
     </list>
     <recent_temporary>
       <list>
+        <item itemvalue="HTTP Request.rest-api_11 | #1" />
+        <item itemvalue="HTTP Request.rest-api_10 | #1" />
+        <item itemvalue="HTTP Request.rest-api_9 | #1" />
+        <item itemvalue="HTTP Request.rest-api_8 | #1" />
         <item itemvalue="JUnit.TestMybatis.testMybatis" />
-        <item itemvalue="JUnit.TestSpringMvc.test" />
       </list>
     </recent_temporary>
   </component>
@@ -288,7 +286,7 @@
       <workItem from="1715994329537" duration="276000" />
       <workItem from="1715994638690" duration="21000" />
       <workItem from="1715994732370" duration="6964000" />
-      <workItem from="1716002556205" duration="2317000" />
+      <workItem from="1716002556205" duration="14734000" />
     </task>
     <task id="LOCAL-00001" summary="0515 springmvc">
       <option name="closed" value="true" />
@@ -298,7 +296,15 @@
       <option name="project" value="LOCAL" />
       <updated>1715763981629</updated>
     </task>
-    <option name="localTasksCounter" value="2" />
+    <task id="LOCAL-00002" summary="0518 整合mybatis">
+      <option name="closed" value="true" />
+      <created>1716004905166</created>
+      <option name="number" value="00002" />
+      <option name="presentableId" value="LOCAL-00002" />
+      <option name="project" value="LOCAL" />
+      <updated>1716004905166</updated>
+    </task>
+    <option name="localTasksCounter" value="3" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
@@ -306,6 +312,7 @@
   </component>
   <component name="VcsManagerConfiguration">
     <MESSAGE value="0515 springmvc" />
-    <option name="LAST_COMMIT_MESSAGE" value="0515 springmvc" />
+    <MESSAGE value="0518 整合mybatis" />
+    <option name="LAST_COMMIT_MESSAGE" value="0518 整合mybatis" />
   </component>
 </project>

+ 19 - 1
springmvc-demo/pom.xml

@@ -105,8 +105,26 @@
             <version>3.0.3</version>
         </dependency>
 
+        <!-- springmvc使用的序列化方式 -->
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.17.0</version>
+        </dependency>
+
     </dependencies>
     <build>
-        <finalName>springmvc-demo-new</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.11.0</version>
+                <configuration>
+                    <parameters>true</parameters>
+                    <source>17</source>
+                    <target>17</target>
+                </configuration>
+            </plugin>
+        </plugins>
     </build>
 </project>

+ 3 - 0
springmvc-demo/src/main/java/com/sf/controller/BookController.java

@@ -45,4 +45,7 @@ public class BookController {
         return modelAndView;
     }
     // http://localhost:8080/springmvc_demo_war/book/other
+
+
+    // http://localhost:8080/springmvc_demo/addBook?bookName=Java&authorName=LiGang
 }

+ 34 - 0
springmvc-demo/src/main/java/com/sf/controller/HeaderController.java

@@ -0,0 +1,34 @@
+package com.sf.controller;
+
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/header")
+public class HeaderController {
+
+    // http://localhost:8080/springmvc_demo/header/header1
+    @RequestMapping("/header1")
+    public String header1(HttpServletRequest request) {
+        // 从请求头中获取指定key
+        String userAgent = request.getHeader("User-Agent");
+        // 浏览器访问结果
+        // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
+        System.out.println(userAgent);
+        // httpclient访问结果
+        // IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+        // postcat访问结果
+        // header1:Postcat
+        return "header1:" + userAgent;
+    }
+
+    // http://localhost:8080/springmvc_demo/header/header2
+    @RequestMapping("/header2")
+    public String header2(@RequestHeader("User-Agent") String userAgent) {
+        System.out.println(userAgent);
+        return "header2:" + userAgent;
+    }
+
+}

+ 33 - 0
springmvc-demo/src/main/java/com/sf/controller/JsonController.java

@@ -0,0 +1,33 @@
+package com.sf.controller;
+
+import com.sf.entity.Book;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.ModelAndView;
+
+import java.util.List;
+
+@Controller
+//@ResponseBody
+public class JsonController {
+
+    // 如果要返回的数据 是一个类 实际要返回的是序列化后的string数据
+    // 具体的序列化方式是  转化为json
+    // http://localhost:8080/springmvc_demo/book
+    @GetMapping("/book")
+    @ResponseBody
+    public Book book() {
+        Book book = Book.builder().bookName("Spring in Action").authorName("Sth").build();
+        return book;
+    }
+
+//    // http://localhost:8080/springmvc_demo/hello
+//    @GetMapping("/hello")
+//    public ModelAndView hello() {
+//        ModelAndView modelAndView = new ModelAndView();
+//        modelAndView.setViewName("/hello.jsp");
+//        return modelAndView;
+//    }
+}

+ 84 - 0
springmvc-demo/src/main/java/com/sf/controller/ParamController.java

@@ -0,0 +1,84 @@
+package com.sf.controller;
+
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/param")
+public class ParamController {
+
+    // http://localhost:8080/springmvc_demo/param/param1?username=zhangsan&password=123456
+    @RequestMapping("/param1")
+    public String param1(HttpServletRequest request) {
+        String username = request.getParameter("username");
+        String password = request.getParameter("password");
+        System.out.println(username + ": " + password);
+        return "param1";
+    }
+
+    // http://localhost:8080/springmvc_demo/param/param?username=zhangsan&password=123456
+    @RequestMapping("/param")
+    public String param(String username, String password) {
+        System.out.println(username + ": " + password);
+        return "param";
+    }
+
+    // http://localhost:8080/springmvc_demo/param/param2?username=zhangsan&password=123456
+    @RequestMapping("/param2")
+    public String param2(@RequestParam("username") String username,
+                         @RequestParam("password") String password) {
+        System.out.println(username + ": " + password);
+        return "param2";
+    }
+
+    // http://localhost:8080/springmvc_demo/param/param3?username=zhangsan&password=123456
+    @RequestMapping("/param3")
+    public String param3(@RequestParam("username") String name,
+                         @RequestParam("password") String pwd) {
+        System.out.println(name + ": " + pwd);
+        return "param3";
+    }
+
+    // required代表参数是否必须 默认是true 可以设置为false
+    // http://localhost:8080/springmvc_demo/param/param4?username=zhangsan&password=123456
+    // http://localhost:8080/springmvc_demo/param/param4?username=zhangsan
+    @RequestMapping("/param4")
+    public String param4(@RequestParam(value = "username", required = true) String name,
+                         @RequestParam(value = "password", required = false) String pwd) {
+        System.out.println(name + ": " + pwd);
+        return "param4";
+    }
+
+    // defaultValue是设定默认值 如果参数传过来了 使用传来的参数  如果参数没有传递 使用默认值
+    // http://localhost:8080/springmvc_demo/param/param5?username=zhangsan
+    @RequestMapping("/param5")
+    public String param5(@RequestParam(value = "username", required = true) String name,
+                         @RequestParam(value = "password", required = false, defaultValue = "654321") String pwd) {
+        System.out.println(name + ": " + pwd);
+        return "param5";
+    }
+
+    // @RequestParam 是从url路径?后面key=value&key1=value1这部分获取参数
+    // @PathVariable 是从url路径中获取参数的注解
+    // http://localhost:8080/springmvc_demo/param/param6/{id}
+    // http://localhost:8080/springmvc_demo/param/param6/111
+    @RequestMapping("/param6/{id}")
+    public String param6(@PathVariable("id") String id) {
+        System.out.println("id: " + id);
+        return "param6";
+    }
+
+    // 按住ctrl 点击即可跳转
+    // http://localhost:8080/springmvc_demo/param/param7/{id}/{name}
+    // http://localhost:8080/springmvc_demo/param/param7/abc/zhangsan
+    // @PathVariable 支持正则表达式的获取  支持多层拼接
+    @RequestMapping("/param7/{id:[a-z]+}/{name}")
+    public String param7(@PathVariable("id") String id, @PathVariable("name") String name) {
+        System.out.println("id: " + id);
+        System.out.println("name: " + name);
+        return "param7";
+    }
+}

+ 26 - 19
springmvc-demo/src/main/java/com/sf/controller/RequestController.java

@@ -1,57 +1,64 @@
 package com.sf.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.RequestParam;
+import org.springframework.web.bind.annotation.*;
 
-@Controller
+// 这里的@Controller代表类中的方法 返回的是页面
+// 而@RestController代表返回的是数据
+@RestController
+//@Controller
 public class RequestController {
 
     //    @RequestMapping(value = "/request",method = RequestMethod.GET)
     @GetMapping("/request")
     public String get() {
-        return "hello";
+        return "hello GetMapping";
     }
 
-    @RequestMapping(value = "/request", method = RequestMethod.POST)
+//    @RequestMapping(value = "/request", method = RequestMethod.POST)
+    @PostMapping("/request")
     public String post() {
-        return "hello";
+        return "hello PostMapping";
     }
 
-    @RequestMapping(value = "/request", method = RequestMethod.PUT)
+//    @RequestMapping(value = "/request", method = RequestMethod.PUT)
+    @PutMapping("/request")
     public String put() {
-        return "hello";
+        return "hello PutMapping";
     }
 
-    @RequestMapping(value = "/request", method = RequestMethod.DELETE)
+//    @RequestMapping(value = "/request", method = RequestMethod.DELETE)
+    @DeleteMapping("/request")
     public String delete() {
-        return "hello";
+        return "hello DeleteMapping";
     }
 
     // 支持ant风格的路径
     // ? 代表任意单个字符
     // * 代表任意多个字符
     // ** 代表任意层数的任意目录
-    @RequestMapping(value = {"/requestAll", "/requestOther", "/requestSth","/request/*"})
+    // http://localhost:8080/springmvc_demo/requestAll
+    // http://localhost:8080/springmvc_demo/requestOther
+    // http://localhost:8080/springmvc_demo/requestSth
+    @RequestMapping(value = {"/requestAll", "/requestOther", "/requestSth"})
     public String all() {
-        return "hello";
+        return "hello value[]";
     }
 
+    // http://localhost:8080/springmvc_demo/requestHeader
     @RequestMapping(value = "/requestHeader",headers = "content-type=text/plain")
     public String header(){
-        return "hello";
+        return "hello header";
     }
 
 
     // params 限定请求的参数
-    // http://localhost:8080/requestParams?id=111
-    // http://localhost:8080/requestParams?id=222
-    // http://localhost:8080/requestParams?id=333
+    // http://localhost:8080/springmvc_demo/requestParams?id=111
+    // http://localhost:8080/springmvc_demo/requestParams?id=222
+    // http://localhost:8080/springmvc_demo/requestParams?id=333
     @RequestMapping(value = "/requestParams",params = "id=111")
     public String params(@RequestParam("id") String id){
-        return "hello";
+        return "hello params";
     }
 
 }