I have this old endpoint that I am trying to upgrade to using spring boot. It is currently used by a few services so I would like to keep it the same.
The old endpoint accepts a Request Body of userName=1234&password=1234
.
As an example of how it is called is curl -X POST "http://localhost:8080/AuthService/login" -H "accept: */*" -H "Content-Type: application/json" -d "userName=123&password=123"
How can I get spring to take the Request Body exactly like my old endpoint but map the input into an Object for me?
The old endpoint was using Apache Access and is like this. It was configured to transform any public method in the class into an endpoint.
public RequestStatus login(String userName, String password) {
...
}
I have translated it over to Spring Boot like this
@ApiOperation(value = "To login", response = RequestStatus.class)
@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<ReturnValue> login(@RequestBody() String info) {
Login login = new Login(info);
....
}
public class Login {
private String password;
private String userName;
public Login(String info) {
String[] values = info.split("&");
for (String value : values) {
String[] pair = value.split("=");
if (pair.length == 2) {
switch (pair[0]) {
case "password":
password = pair[1];
break;
case "userName":
userName = pair[1];
break;
}
}
}
}
}
But that method of transforming the Request Body into the object is really ugly and error prone and I would like to improve it.