Skip to content
Advertisement

Getting a list of JSON Users objs. in a POST request and return a response also as JSON

This is my first time using spring-webflux with SpringBoot. My goal is to create an API that accepts a list of Users in JSON format in the body of a POST and return a response with that list also in JSON format ordered by a date field. The ordering is not a problem but I do not know very well how to make this architecture. So far I have:

  • UserWebfluxApplication (with the @SpringBootApplication)

  • model/User.java (id, name, surname, creationDate)

  • service/UserService.java (Interface)

  • service/UserServiceImpl.java implementing the Interface with the WebClient and with the following method:

      @Override
      public List<User> processUserDataFromUserList() {
              Mono<List<User>> response = webClient.get()
              .accept(MediaType.APPLICATION_JSON)
              .retrieve()
              .bodyToMono(new ParameterizedTypeReference<List<User>>() {});
    
              List<User> users = response.block();
    
              return users .stream()
              .sorted(Comparator.comparing(User::getCreatedTime))
              .collect(Collectors.toList());
      }
    
  • controller/UserController.java (Here I don’t know how can I construct this class for reach my achievement testing the request for example with curl, Postman, or whatever). So far I’ve this in the UserController:

      @RestController
      public class UserController {
    
          @Autowired
          private UserService userService;
    
          @PostMapping(value = {"/readUser"})
          @ResponseStatus(HttpStatus.OK)
          public void readUser(@RequestBody List<User> users) {
              userService.processUserDataFromUserList();
          }
    
      }
    

The json request will be like:

  {
    "id": 951,
    "name": "Marco",
    "surname": "Polo",
    "creationDate": "2021-01-23T02:36:09-08:00"
  },
  {
    "id": 952,
    "name": "Estela",
    "surname": "Star",
    "creationDate": "2021-01-14T18:30:09-07:00"
  },
  {
    "id": 953,
    "name": "Jim",
    "surname": "Pask",
    "creationDate": "2021-01-02T08:22:12+05:00"
  },
  {
    "id": 954,
    "name": "Lorena",
    "surname": "Cubo",
    "creationDate": "2021-02-15T01:01:32-02:00"
  }

Thank you in advance.

Advertisement

Answer

Finally I’ve solved it by the following:

Controller:

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = {"/getByCreatedTimeAsc"})
    @ResponseStatus(HttpStatus.OK)
    public Flux<User> readUsers(@RequestBody List<User> users) {
        return userService.processUserDataFromUserList(users);
    }
}

Service:

@Service
public class UserServiceImpl implements UserService {

    @Override
    public Flux<User> processUserDataFromUserList(List<User> users) {
        return Flux.fromIterable(users.stream()
                .sorted(Comparator.comparing(User::getCreatedTime))
                .collect(Collectors.toList()));
    }

}

The main problem I was facing is that I was trying to manage the list with Mono rather than Flux.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement