Reputation: 848
I'm trying to check if the username is unique in spring-boot. I want to send the result as JSON object. This is the REST controller
@RequestMapping(value="/checkEmailUnique",method=RequestMethod.POST)
public String checkEmailUnique(@RequestBody String username){
AppUser app = userRepo.findByUsername(username);
if(app!=null){
// I want to return somthing like emailNotTaken: true
}
else{
// and here : emailNotTaken: false
}
}
I want to get the result in angular so I can show an error message in my component. How can I do that?
Angular side
client.Service.Ts
checkEmailNotTaken(email:string){
if(this.authService.getToken()==null) {
this.authService.loadToken();
}
return this.http.post(this.host+
"/checkEmailUnique/",{email},{headers:new HttpHeaders({'Authorization':this.authService.getToken()})});
}
in client.component.ts
ngOnInit() {
this.form = this.formBuilder.group({
prenom: ['', Validators.required],
nom: ['', Validators.required],
tel: ['', Validators.required],
cin: ['', Validators.required],
username: ['', Validators.required , Validators.email , this.validateEmailNotTaken.bind(this)],
passwordG: this.formBuilder.group({
password: ['',[Validators.required,Validators.minLength(9)]],
Confirmationpassword : ['',[Validators.required,Validators.minLength(9)]]
}, {validator: passwordMatch})
});
}
validateEmailNotTaken(control: AbstractControl) {
return this.clientService.checkEmailNotTaken(control.value).map(res => {
return // what to do here ?
});
}
EDIT
@RequestMapping(value="/checkEmailUnique",method=RequestMethod.POST)
public EmailStatusCheckJson checkEmailUnique(@RequestBody final String username){
final EmailStatusCheckJson returnValue = new EmailStatusCheckJson();
AppUser app = userRepo.findByUsername(username);
if(app!=null){
returnValue.setEmailIsAvailable(false);
}
else{
returnValue.setEmailIsAvailable(true);
}
return returnValue;
}
Upvotes: 3
Views: 15979
Reputation: 609
You could also do this way using ResponseEntity as the return value to your RestController
public ResponseEntity<?> checkEmailUnique(@RequestBody String username){
AppUser app = userRepo.findByUsername(username);
if(null != app) {
return ResponseEntity.badRequest().build(); // Will return a 400 response
}
return ResponseEntity.ok().build(); // Will return a 200 response
}
Base on the response type you could directly identify if the email exits or not instead of returning json in this case.
Upvotes: 1
Reputation: 38300
If you are using the spring-boot-starter-web,
your project is already set to return JSON.
Instead of String
as the return value from checkEmailUnique
,
use an object type that you create.
Here is an example:
public class EmailStatusCheckJson
{
private Boolean emailIsAvailable;
public Boolean getEmailIsAvailable()
{
return emailIsAvailable;
}
public void setEmailIsAvailable(
final Boolean newValue)
{
emailIsAvailable = newValue
}
}
@RequestMapping(value="/checkEmailUnique",method=RequestMethod.POST)
public EmailStatusCheckJson checkEmailUnique(@RequestBody final String username)
{
final EmailStatusCheckJson returnValue = new EmailStatusCheckJson();
if (...) // email is available.
{
returnValue.setEmailIsAvailable(true);
}
else
{
returnValue.setEmailIsAvailable(false);
}
return returnValue;
}
Edited added more example.
Upvotes: 6
Reputation: 289
Rest method have a return type as String .In place of any String you can use a user defined object where(inside there) put a boolean variable e.g. status there you can send whether the username is present or not. According to the response from the rest side you can forward towards angular.
Upvotes: 1