Spring 引导 - 如何在 HTTP Post api 中传递自定义值?
Spring Boot - How can I pass custom values in HTTP Post api?
我是 Spring Boot 的新手,我很难理解如何传递数据。例如:
我想将这些数据传递到我的服务器:
{
"code", 1,
"name": "C01"
}
所以我创建了 always 一个自定义对象,将代码和名称作为属性来拥有这个 http post api?
@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
...
}
我看到的另一个解决方案可以是这个,但我不能传递数字(int 代码),对吗?
@RequestMapping(value = "/new/{code}/{name}", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
...
}
亲切的问候:)
您可以将 code
和 name
作为 PathVariable
传递,就像在您的示例中一样:
@RequestMapping(value = "/new/{code}/{name}")
public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
...
}
A PathVariable
可以是 int 或 String 或 long 或 Date,根据文档:
A @PathVariable argument can be of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws a TypeMismatchException if it fails to do so.
您还可以像这样定义 Map<String, Object>
类型的 PathVariable
:
@RequestMapping(value = "/new/{code}/{name}")
public ResponseEntity<?> createOrder(@PathVariable("map") Map<String, Object> map) {
Integer code = (Integer) map.get("code");
String name = (String) map.get("name");
...
}
您甚至可以使用 @RequestParam
并以 URL 查询参数的形式提供数据。
因此,可以通过多种方式将数据传递给 Spring MVC 控制器(更多详细信息 in the docs),但我认为 发布的约定 复杂数据("complex" 我的意思是不止一个状态)是定义一个请求主体,其中包含该复杂状态的序列化形式,即您在问题的第一个示例中显示的内容:
@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
...
}
如果这个问题是关于 RESTful 最佳实践的,因为您正在开发用于创建 Order 对象的网络服务,这就是我设计它的方式
Order.java
public class Order {
private Integer code;
private String name;
public Integer getCode() {
return code;
}
public void setCode(final Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
控制器
@RequestMapping(value = "/orders", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Order> createOrder(@Valid @RequestBody Order order){
...
}
从技术上讲,您可以做很多事情来实现同一件事,但这不会是一个 RESTful 服务,它最多是一个 RPC。
我是 Spring Boot 的新手,我很难理解如何传递数据。例如:
我想将这些数据传递到我的服务器:
{
"code", 1,
"name": "C01"
}
所以我创建了 always 一个自定义对象,将代码和名称作为属性来拥有这个 http post api?
@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
...
}
我看到的另一个解决方案可以是这个,但我不能传递数字(int 代码),对吗?
@RequestMapping(value = "/new/{code}/{name}", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
...
}
亲切的问候:)
您可以将 code
和 name
作为 PathVariable
传递,就像在您的示例中一样:
@RequestMapping(value = "/new/{code}/{name}")
public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
...
}
A PathVariable
可以是 int 或 String 或 long 或 Date,根据文档:
A @PathVariable argument can be of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws a TypeMismatchException if it fails to do so.
您还可以像这样定义 Map<String, Object>
类型的 PathVariable
:
@RequestMapping(value = "/new/{code}/{name}")
public ResponseEntity<?> createOrder(@PathVariable("map") Map<String, Object> map) {
Integer code = (Integer) map.get("code");
String name = (String) map.get("name");
...
}
您甚至可以使用 @RequestParam
并以 URL 查询参数的形式提供数据。
因此,可以通过多种方式将数据传递给 Spring MVC 控制器(更多详细信息 in the docs),但我认为 发布的约定 复杂数据("complex" 我的意思是不止一个状态)是定义一个请求主体,其中包含该复杂状态的序列化形式,即您在问题的第一个示例中显示的内容:
@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
...
}
如果这个问题是关于 RESTful 最佳实践的,因为您正在开发用于创建 Order 对象的网络服务,这就是我设计它的方式
Order.java
public class Order {
private Integer code;
private String name;
public Integer getCode() {
return code;
}
public void setCode(final Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
控制器
@RequestMapping(value = "/orders", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Order> createOrder(@Valid @RequestBody Order order){
...
}
从技术上讲,您可以做很多事情来实现同一件事,但这不会是一个 RESTful 服务,它最多是一个 RPC。