I see a couple of things here that you should fix. Let's start talking about the REST standard, the first rule is to think in endpoints as representation of resources, not operations, for example, in your code, I presume the MyObject
class represents a Point (you should refactor the class to have a proper name), then the path value for the getObject
can be "/point". The operations are mapped on the HTTP method, accordingly:
- GET: Obtain info about a resource.
- POST: Create a resource.
- PUT: Update a resource.
- DELETE: Delete a resource.
In getObject
you're expecting to receive an object. The get method according to the REST standards means you want to retrieve some data, and usually you send some data included in the url like ../app-context/get/{id}, here the id is a parameter that tells your controller you want some info belonging to an id, so if you would invoke the endpoint like as ../app-context/get/1 to get info of some domain object identified by the number 1.
If you want to send data to the server, the most common HTTP method is a POST.
According to this, at design level you should:
- Give a meaningful name to the MyObject class.
- Check the operation you want to make in the
getObject
.
- Assign a path to
getObject
representing a resource.
At code level, with the above comments, you could change this as:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyObject {
@Min(-180) @Max(180)
private double x;
@Min(-90) @Max(90)
private double y;
}
@PostMapping("/point")
public ResponseEntity savePoint(@RequestBody @Valid MyObject myObject) {...}
I will explain the changes:
- Add @PostMapping to fulfill the REST standard.
- Add @RequestBody, this annotation take the info sent to the server and use it to create a MyObject object.
- Add @NoArgsConstructor to MyObject, by default, the deserialisation use a default constructor (with no arguments). You could write some specialised code to make the things work without the default constructor, but thats up to you.
@RestController
or some Spring Data controller? There is some discussion below on@RestController
vs@RepositoryRestController
and different ways to get validation working there. – Earth