diff --git a/spring-boot-helloworld/pom.xml b/spring-boot-helloworld/pom.xml
new file mode 100644
index 0000000..bed6fdb
--- /dev/null
+++ b/spring-boot-helloworld/pom.xml
@@ -0,0 +1,62 @@
+
+
User: Zhang Kaitao + *
Date: 13-12-22 + *
Version: 1.0 + */ +@EnableAutoConfiguration +@Configuration +@ComponentScan +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class); + } +} diff --git a/spring-boot-helloworld/src/main/java/com/sishuok/controller/UserController.java b/spring-boot-helloworld/src/main/java/com/sishuok/controller/UserController.java new file mode 100644 index 0000000..d93299e --- /dev/null +++ b/spring-boot-helloworld/src/main/java/com/sishuok/controller/UserController.java @@ -0,0 +1,31 @@ +package com.sishuok.controller; + +import com.sishuok.entity.User; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + *
User: Zhang Kaitao + *
Date: 13-12-22 + *
Version: 1.0 + */ +@EnableAutoConfiguration +@RestController +@RequestMapping("/user") +public class UserController { + + @RequestMapping("/{id}") + private User view(@PathVariable("id") Long id) { + User user = new User(); + user.setId(id); + user.setName("zhang"); + return user; + } + + public static void main(String[] args) { + SpringApplication.run(UserController.class); + } +} diff --git a/spring-boot-helloworld/src/main/java/com/sishuok/entity/User.java b/spring-boot-helloworld/src/main/java/com/sishuok/entity/User.java new file mode 100644 index 0000000..54c2073 --- /dev/null +++ b/spring-boot-helloworld/src/main/java/com/sishuok/entity/User.java @@ -0,0 +1,29 @@ +package com.sishuok.entity; + +import java.io.Serializable; + +/** + *
User: Zhang Kaitao + *
Date: 13-12-22 + *
Version: 1.0
+ */
+public class User implements Serializable {
+ private Long id;
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/springmvc-rest-helloworld/README b/springmvc-rest-helloworld/README
new file mode 100644
index 0000000..7ea0b38
--- /dev/null
+++ b/springmvc-rest-helloworld/README
@@ -0,0 +1 @@
+ 5分钟构建spring web mvc Rest风格HelloWorld
\ No newline at end of file
diff --git a/springmvc-rest-helloworld/pom.xml b/springmvc-rest-helloworld/pom.xml
new file mode 100644
index 0000000..8b1c2ae
--- /dev/null
+++ b/springmvc-rest-helloworld/pom.xml
@@ -0,0 +1,67 @@
+
+
User: Zhang Kaitao + *
Date: 13-12-19 + *
Version: 1.0 + */ +@Configuration +@EnableWebMvc +@ComponentScan +public class AppConfig { +} diff --git a/springmvc-rest-helloworld/src/main/java/com/sishuok/AppInitializer.java b/springmvc-rest-helloworld/src/main/java/com/sishuok/AppInitializer.java new file mode 100644 index 0000000..cc26004 --- /dev/null +++ b/springmvc-rest-helloworld/src/main/java/com/sishuok/AppInitializer.java @@ -0,0 +1,31 @@ +package com.sishuok; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +/** + *
User: Zhang Kaitao + *
Date: 13-12-19 + *
Version: 1.0 + */ +public class AppInitializer implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + AnnotationConfigWebApplicationContext webApplicationContext = + new AnnotationConfigWebApplicationContext(); + webApplicationContext.register(AppConfig.class); + + + DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext); + ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet); + dynamic.addMapping("/"); + + } +} diff --git a/springmvc-rest-helloworld/src/main/java/com/sishuok/controller/UserController.java b/springmvc-rest-helloworld/src/main/java/com/sishuok/controller/UserController.java new file mode 100644 index 0000000..3ae1d49 --- /dev/null +++ b/springmvc-rest-helloworld/src/main/java/com/sishuok/controller/UserController.java @@ -0,0 +1,25 @@ +package com.sishuok.controller; + +import com.sishuok.entity.User; +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; + +/** + *
User: Zhang Kaitao + *
Date: 13-12-19 + *
Version: 1.0 + */ +@RestController +@RequestMapping("/user") +public class UserController { + + @RequestMapping(value = "/{id}", method = RequestMethod.GET) + public User view(@PathVariable("id") Long id) { + User user = new User(); + user.setId(id); + user.setName("zhang"); + return user; + } +} diff --git a/springmvc-rest-helloworld/src/main/java/com/sishuok/entity/User.java b/springmvc-rest-helloworld/src/main/java/com/sishuok/entity/User.java new file mode 100644 index 0000000..d46cb19 --- /dev/null +++ b/springmvc-rest-helloworld/src/main/java/com/sishuok/entity/User.java @@ -0,0 +1,46 @@ +package com.sishuok.entity; + +import java.io.Serializable; + +/** + *
User: Zhang Kaitao + *
Date: 13-12-19 + *
Version: 1.0 + */ +public class User implements Serializable { + private Long id; + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + User user = (User) o; + + if (id != null ? !id.equals(user.id) : user.id != null) return false; + + return true; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +}