I consider using fluent-http in a project.
I started with a simple "login/password" page. I create a simple POJO with fields login
and password
:
public class LoginRequest() {
private String login;
private String password;
//...
}
And I send it to fluent-http via a Resource
:
@Prefix("/user")
public class PersonResource {
@Post("/")
public String get(LoginRequest loginRequest) {
//[...]
}
}
And it works well :)
Now, I wondered if it was possible to send a response with code HTTP 200 in case of success and code HTTP 401 in case of failure.
So I tried to inject the Response
:
@Post("/")
public String login(LoginRequest loginRequest, Response response) {
if(loginRequest.getPassword().equals("helloworld")) {
response.setStatus(200);
return "SUCCESS";
} else {
response.setStatus(401);
return "ERROR";
}
}
The correct String is returned but the status code does not seem to be used. In both cases, the response has a code HTTP 200.
Note : I found that some status code are pre-implemented :
Any idea?
If you want to change the default content-type, status or headers, the method should return a net.codestory.http.payload.Payload
.
Here's what you should write:
@Post("/")
public Payload login(LoginRequest loginRequest) {
if(!loginRequest.getPassword().equals("helloworld")) {
return new Payload("ERROR").withCode(HttpStatus.UNAUTHORIZED);
}
return new Payload("SUCCESS").withCode(HttpStatus.CREATED);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.