0
@GetMapping("/getAccount")
public Account validateAccount(@RequestBody) {
}

Very new to spring boot. My account file has 5+ values all strings, username, password, id, and some etc things.

Given this

{
    "username": "bob"
    "password": "password"
}

It should give this with 200 response code OK

{
    "id": "45645646546"
    "username": "bob"
    "password": "password"
    "status": "Single"
    "filler": "filler" 
}

However I'm not sure how to read the "username" and "password" json in my validateAccount function

Not really related to this question but does anyone know how to send a response code in the function? Like .sendresponseheader(400) something like that

bob
  • 75
  • 4

3 Answers3

2
public class AccountDTO {
    @JsonIgnore
    private Long id;
    @NotNull
    private String username;
    @NotNull
    private String password;
    @JsonIgnore
    private String status;
    @JsonIgnore
    private String filler;

    // getters & setters
}

You may want to create a DTO (Data Transaction Object) as shown above. Here's a link to it's wiki.

Next pass map user input into this DTO using @RequestBody annotation.

@RestController
public class AccountController {
    @GetMapping("/accounts")
    public ResponseEntity<Account> validateAccount(@RequestBody AccountDTO accountDTO) {
        return new ResponseEntity<>(accountService.validate(accountDTO), HttpStatus.OK);
    }
}

Or you can use

@RestController
public class AccountController {
    @GetMapping("/accounts")
    public Response validateAccount(@RequestBody AccountDTO accountDTO) {
        return new ResponseEntity().ok(accountService.validate(accountDTO));
    }
}

The user input will be converted from json to AccountDTO using whatever JSON processor your're using most probably it'll be com.fasterxml.jackson.core.

The @JsonIgnore and @NotNull annotation will ensure only username and password fields are used and others are ignored while taking input from user.

You can pass this DTO to your service classes and use something like findByUsername() in your Business Logic and return populated AccountDTO using the below mapper function or some external libraries like Model Mapper or MapStruct.

public toAccountDTO(Account account) {
    AccountDTO accountDTO = new AccountDTO();
    accountDTO.setUsername(account.getUsername());
    // and so on...
    return accountDTO;
}

And for your last query, wrap the returned AccountDTO object in ResponseEntity wrapper to provide a proper Response Code with your payload. Here's a link to ResponseEntity Java docs.

1
AccountDto.java
===============
class AccountDto{   
    private Long id;
    private String username;
    private String password;
    private String status;
    private String filler;

    //getters & setters
}



@GetMapping("/getAccount")
public ResponseEntity validateAccount(@RequestBody AccountDto accountDto) {
    return new ResponseEntity<>(accountServie.validate(accountDto),HttpStatus.OK);
}   

You can do your custom operations before returning the response. Take a look Best Practice of REST

DEBENDRA DHINDA
  • 1,163
  • 5
  • 14
0

For json response nothing specific just mark class with @RestController.

For @RequestBody just use a pojo to bind the values

For error code and status you can use ResponseEntity

Niraj Jha
  • 455
  • 3
  • 10