1 | http: //localhost:8080/hello/hangge |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello/{name}" ) public String hello( @PathVariable ( "name" ) String name) { return "获取到的name是:" + name; } } |
1 | http: //localhost:8080/hello?name=hangge |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @RequestParam ( "name" ) String name) { return "获取到的name是:" + name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @RequestParam (name = "name" , required = false ) String name) { return "获取到的name是:" + name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @RequestParam (name = "name" , defaultValue = "xxx" ) String name) { return "获取到的name是:" + name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example.demo; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import java.util.Map; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @RequestParam Map<String, Object> params) { return "name:" + params.get( "name" ) + "<br>age:" + params.get( "age" ); } } |
1 | http: //localhost:8080/hello?name=hangge&name=google |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.demo; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @RequestParam ( "name" ) String[] names) { String result = "" ; for (String name:names){ result += name + "<br>" ; } return result; } } |
1 2 3 4 5 6 7 8 9 10 11 12 | package com.example.demo; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello(User user) { return "name:" + user.getName() + "<br> age:" + user.getAge(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.example.demo; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.demo; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello( @ModelAttribute ( "u" ) User user) { return "name:" + user.getName() + "<br> age:" + user.getAge(); } @InitBinder ( "u" ) private void initBinder(WebDataBinder binder) { binder.setFieldDefaultPrefix( "u." ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping ( "/hello" ) public String hello(User user, Phone phone) { return "name:" + user.getName() + "<br> age:" + user.getAge() + "<br> number:" + phone.getNumber(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo; public class Phone { private String number; public String getNumber() { return number; } public void setNumber(String number) { this .number = number; } } |
联系客服