3

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 :)

glytching
  • 44,936
  • 9
  • 114
  • 120
Nammen8
  • 619
  • 1
  • 11
  • 31

2 Answers2

1

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){
    ...
}
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Perfect, thanks... so if I want use Map I have to pass a JSON object? Like { "code", 1, "name": "C01"}? Thanks :) – Nammen8 Sep 10 '17 at 10:01
  • 1
    Yes, that's possible but just to reiterate: "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" this is especially the case in REST APIs since that complex state would often be represented by an entity (i.e. a class) on the server side. So, I think your original approach of defining a custom object is more common than posting a `Map`. Hope that helps. – glytching Sep 10 '17 at 10:37
1

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.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • Hi, thanks but if code is for the Order and name is for the Client, I have a create this new object (with code and name) to pass those data (without usign @PathVariable)? Thanks again – Nammen8 Sep 10 '17 at 09:58
  • @Hazlo8 Can you explain a bit more about your requirement – so-random-dude Sep 10 '17 at 14:38