Browse Source

springMVC 练习 restful等

zhang 2 years ago
parent
commit
fe041352bf

+ 5 - 1
springMvc/pom.xml

@@ -86,7 +86,11 @@
86 86
       <artifactId>jackson-annotations</artifactId>
87 87
       <version>2.9.8</version>
88 88
     </dependency>
89
-
89
+    <dependency>
90
+      <groupId>org.projectlombok</groupId>
91
+      <artifactId>lombok</artifactId>
92
+      <version>1.18.24</version>
93
+    </dependency>
90 94
   </dependencies>
91 95
 
92 96
 </project>

+ 49 - 0
springMvc/src/main/java/com/controller/ParamController.java

@@ -0,0 +1,49 @@
1
+package com.controller;
2
+
3
+import com.pojo.Student;
4
+import org.springframework.stereotype.Controller;
5
+import org.springframework.web.bind.annotation.RequestBody;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestParam;
8
+import org.springframework.web.bind.annotation.ResponseBody;
9
+
10
+import java.util.List;
11
+
12
+@Controller
13
+public class ParamController {
14
+    // 基本类型参数
15
+    // http://localhost:8083/param1?name=zxy&age=11
16
+    @RequestMapping("/param1")
17
+    @ResponseBody
18
+    public void param1(String name,Integer age) {
19
+        System.out.println(name);
20
+        System.out.println(age);
21
+    }
22
+    // 获取pojo类型的参数
23
+    // http://localhost:8083/param2?name=zxy&age=11
24
+    @RequestMapping("param2")
25
+    @ResponseBody
26
+    public void param2(Student student) {
27
+        System.out.println(student.getAge());
28
+    }
29
+    // 获取数组类型的参数
30
+    // http://localhost:8083/param1?name=zxy&name=zxy2&name=zxy3
31
+    @RequestMapping("param3")
32
+    @ResponseBody
33
+    public void param3(String[] name) {
34
+        System.out.println(name);
35
+    }
36
+    // 获取集合类型参数
37
+    @RequestMapping("param4")
38
+    @ResponseBody
39
+    public void param4(@RequestBody List<Student> students) {
40
+        System.out.println(students);
41
+    }
42
+    // 使用RequestParam
43
+    // http://localhost:8083/param5?Name=%E5%BC%A0
44
+    @RequestMapping("param5")
45
+    @ResponseBody
46
+    public void param5(@RequestParam(name = "Name1",required = false) String name) {
47
+        System.out.println(name);
48
+    }
49
+}

+ 36 - 0
springMvc/src/main/java/com/controller/RestfulController.java

@@ -0,0 +1,36 @@
1
+package com.controller;
2
+
3
+import org.springframework.stereotype.Controller;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+import javax.servlet.http.HttpSession;
7
+import java.util.Date;
8
+
9
+@Controller
10
+// restful风格接口测试
11
+public class RestfulController {
12
+    //http://localhost:8083/rest1/zxy/19
13
+    @RequestMapping("/rest1/{name}/{age}")
14
+    @ResponseBody
15
+    public void rest1 (@PathVariable(value = "name") String name,
16
+                       @PathVariable(value = "age") Integer age) {
17
+        System.out.println(name);
18
+    }
19
+    // http://localhost:8083/rest2?date=2019-12-01
20
+    @RequestMapping("/rest2")
21
+    @ResponseBody
22
+    public void rest1 (@RequestParam(name = "date") Date date, HttpSession session) {
23
+        System.out.println(date);
24
+        System.out.println(session);
25
+    }
26
+    @RequestMapping("/rest3")
27
+    @ResponseBody
28
+    public void rest3 (@RequestHeader(value = "User-Agent") String userAgent) {
29
+        System.out.println(userAgent);
30
+    }
31
+    @RequestMapping("/rest4")
32
+    @ResponseBody
33
+    public void rest4 (@CookieValue(value = "JSESSIONID") String sessionId) {
34
+        System.out.println(sessionId);
35
+    }
36
+}

+ 22 - 0
springMvc/src/main/java/com/converter/DateConverter.java

@@ -0,0 +1,22 @@
1
+package com.converter;
2
+
3
+import org.springframework.core.convert.converter.Converter;
4
+
5
+import java.text.ParseException;
6
+import java.text.SimpleDateFormat;
7
+import java.util.Date;
8
+/**
9
+ * 时间日期转换器
10
+ */
11
+public class DateConverter implements Converter<String, Date> {
12
+    public Date convert(String s) {
13
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
14
+        Date date = null;
15
+        try {
16
+            date = simpleDateFormat.parse(s);
17
+        } catch (ParseException e) {
18
+            e.printStackTrace();
19
+        }
20
+        return date;
21
+    }
22
+}

+ 4 - 8
springMvc/src/main/java/com/pojo/Student.java

@@ -1,13 +1,9 @@
1 1
 package com.pojo;
2 2
 
3
+import lombok.Data;
4
+
5
+@Data
3 6
 public class Student {
4 7
     public String name;
5
-
6
-    public String getName() {
7
-        return name;
8
-    }
9
-
10
-    public void setName(String name) {
11
-        this.name = name;
12
-    }
8
+    public Integer age;
13 9
 }

+ 12 - 1
springMvc/src/main/resources/Spring-mvc.xml

@@ -36,5 +36,16 @@
36 36
 <!--        </property>-->
37 37
 
38 38
 <!--    </bean>-->
39
-        <mvc:annotation-driven></mvc:annotation-driven>
39
+        <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
40
+        <!--  开放静态资源的访问-->
41
+        <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
42
+<!--        声明转换器-->
43
+        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
44
+            <property name="converters">
45
+                <list>
46
+                    <bean class="com.converter.DateConverter"></bean>
47
+                </list>
48
+            </property>
49
+        </bean>
50
+
40 51
 </beans>

+ 1 - 0
springMvc/src/main/webapp/js/index.js

@@ -0,0 +1 @@
1
+console.log("index!")