Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - How can I pass custom values in HTTP Post api?

I'm new with Spring Boot and I have difficult to understand how can I pass data. For example:

I want pass those data to my server:

{
    "code", 1,
    "name": "C01"
}

So I have create always a custom Object with code and name as attributes to have this http post api?

@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
    ...
}

Another solution I see that can be this but I can't pass numbers (int code), right?

@RequestMapping(value = "/new/{code}/{name}", method = RequestMethod.POST)
    public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
        ...
}

Kind regards :)

like image 417
Nammen8 Avatar asked Jan 28 '26 06:01

Nammen8


2 Answers

You can pass code and name as PathVariables just like in your example:

@RequestMapping(value = "/new/{code}/{name}")
public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
    ...
}

A PathVariable can be an int or a String or a long or a Date, according to the docs:

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.

You could also define a PathVariable of type Map<String, Object> like this:

@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");
    ...
}

You could even use @RequestParam and supply the data in the form of URL query parameters.

So, there are numerous ways in which data can be passed to a Spring MVC controller (more details in the docs) but I think the convention for posting complex data (by "complex" I mean more than a single piece of state) is to define a request body which contains a serialised form of that complex state i.e. what you showed in the first example in your queston:

@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
    ...
}
like image 124
glytching Avatar answered Jan 30 '26 22:01

glytching


If this question is about RESTful best practice, since you are developing webservice for creating an Order object, this is how I would design it

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;
  }
}

Controller

@RequestMapping(value = "/orders", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Order> createOrder(@Valid @RequestBody Order order){
    ...
}

Technically, you can do many things to achieve the same thing, but that will not be a RESTful service, it will be an RPC at best.

like image 43
so-random-dude Avatar answered Jan 30 '26 22:01

so-random-dude