Ver Fonte

restful

admin há 1 ano atrás
pai
commit
b243bb0ff2

+ 48 - 0
restfuleSpringBoot/pom.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>restfuleSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 12 - 0
restfuleSpringBoot/src/main/java/org/example/Main.java

@@ -0,0 +1,12 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args){
+        SpringApplication.run(Main.class, args);
+    }
+}

+ 50 - 0
restfuleSpringBoot/src/main/java/org/example/controller/StudentController.java

@@ -0,0 +1,50 @@
+package org.example.controller;
+
+
+import org.example.entitiy.Student;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.*;
+
+@RestController
+public class StudentController {
+
+
+    @RequestMapping(value = "/student")
+    public Object student(Integer id , String name){
+        Student student = new Student();
+        student.setId(id);
+        student.setName(name);
+
+        return student;
+    }
+
+
+
+    @RequestMapping(value = "student/detail/{id}/{name}")
+    public Object student1(@PathVariable("id") Integer id,
+                           @PathVariable("name") String name){
+
+        HashMap<Object, Object> retMap = new HashMap<>();
+
+        retMap.put("id",id);
+        retMap.put("name",name);
+
+        return retMap;
+    }
+
+
+    //    和上面的请求路径冲突
+    @RequestMapping(value = "student/detail/{id}/{status}")
+    public Object student2(@PathVariable("id") Integer id,
+                           @PathVariable("status") String status){
+
+        HashMap<Object, Object> retMap = new HashMap<>();
+
+        retMap.put("id",id);
+        retMap.put("status",status);
+
+        return retMap;
+    }
+
+}

+ 23 - 0
restfuleSpringBoot/src/main/java/org/example/entitiy/Student.java

@@ -0,0 +1,23 @@
+package org.example.entitiy;
+
+public class Student {
+
+    private Integer id;
+    private String name;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}