main v3.0.0
liangjiajie 1 year ago
parent 94dab49cd1
commit dc8d2397c8

@ -17,10 +17,7 @@
package com.ljj.myspringboot.demos.web; package com.ljj.myspringboot.demos.web;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/** /**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a> * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
@ -29,14 +26,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class BasicController { public class BasicController {
// http://127.0.0.1:8080/hello?name=lisi // http://127.0.0.1:8080/hello?name=lisi
@RequestMapping("/hello") @GetMapping("/hello")
@ResponseBody @ResponseBody
public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) { public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
return "Hello v2.0.0 " + name; return "Hello v2.0.0 " + name;
} }
// http://127.0.0.1:8080/user // http://127.0.0.1:8080/user
@RequestMapping("/user") @GetMapping("/user")
@ResponseBody @ResponseBody
public User user() { public User user() {
User user = new User(); User user = new User();
@ -46,14 +43,14 @@ public class BasicController {
} }
// http://127.0.0.1:8080/save_user?name=newName&age=11 // http://127.0.0.1:8080/save_user?name=newName&age=11
@RequestMapping("/save_user") @GetMapping("/save_user")
@ResponseBody @ResponseBody
public String saveUser(User u) { public String saveUser(User u) {
return "user will save: name=" + u.getName() + ", age=" + u.getAge(); return "user will save: name=" + u.getName() + ", age=" + u.getAge();
} }
// http://127.0.0.1:8080/html // http://127.0.0.1:8080/html
@RequestMapping("/html") @GetMapping("/html")
public String html() { public String html() {
return "index.html"; return "index.html";
} }

@ -17,10 +17,7 @@
package com.ljj.myspringboot.demos.web; package com.ljj.myspringboot.demos.web;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/** /**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a> * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
@ -29,14 +26,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class PathVariableController { public class PathVariableController {
// http://127.0.0.1:8080/user/123/roles/222 // http://127.0.0.1:8080/user/123/roles/222
@RequestMapping(value = "/user/{userId}/roles/{roleId}", method = RequestMethod.GET) @GetMapping(value = "/user/{userId}/roles/{roleId}")
@ResponseBody @ResponseBody
public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) { public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {
return "User Id : " + userId + " Role Id : " + roleId; return "User Id : " + userId + " Role Id : " + roleId;
} }
// http://127.0.0.1:8080/javabeat/somewords // http://127.0.0.1:8080/javabeat/somewords
@RequestMapping(value = "/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET) @GetMapping(value = "/javabeat/{regexp1:[a-z-]+}")
@ResponseBody @ResponseBody
public String getRegExp(@PathVariable("regexp1") String regexp1) { public String getRegExp(@PathVariable("regexp1") String regexp1) {
return "URI Part : " + regexp1; return "URI Part : " + regexp1;

@ -1,6 +0,0 @@
<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>

@ -1,13 +1,19 @@
package com.ljj.myspringboot; package com.ljj.myspringboot;
import com.ljj.myspringboot.demos.web.User;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import static org.springframework.test.util.AssertionErrors.assertEquals;
@SpringBootTest @SpringBootTest
class MyspringbootApplicationTests { class MyspringbootApplicationTests {
@Test @Test
void contextLoads() { void contextLoads() {
assertEquals("正确",1,"1==1");
} }
} }

Loading…
Cancel
Save