0
votes

I am trying to use Formatter in webflux application but its throws

java.lang.IllegalStateException: Iterating over a toIterable() / toStream() is blocking, which is not supported in thread reactor-http-nio-2

Exception indicate i can't use block and method is expecting an object of PetType. I wanted to know if there is any other way to do this

@Component
public class PetTypeFormatter implements Formatter<PetType> {

    private final PetTypeService petTypeServive;    

    public PetTypeFormatter(PetTypeService petTypeServive) {
        this.petTypeServive = petTypeServive;
    }

    @Override
    public String print(PetType petType, Locale locale) {
        return petType.getName();
    }


    @Override
    public PetType parse(String text, Locale locale) throws ParseException 
    {


        Iterable<PetType> findPetTypes =    petTypeServive.findAll().toIterable();

        for (PetType type : findPetTypes) 
        {
            if (type.getName().equals(text)) {
                return type;
            }
        }

        throw new ParseException("type not found: " + text, 0);
    }

}

Edit: The method signature of controller which i am using is

@PostMapping("/pets/new")
    public String processCreationForm(@ModelAttribute("owner") Owner owner, @Valid Pet pet,BindingResult result, ModelMap model) 

and the Pet class petType property which i was setting through the custom formatter when using webmvc

Edit2:

@Setter
@Getter
public class Pet
{   
    private String id;
    private PetType petType;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthDate; 
    private String name;
}

@Setter
@Getter
public class PetType
{
    private String name;

    @Override
    public String toString() {
        return name;
    }
}
1
Show how it's used with flux.123
petTypeServive.findAll() is returning FLux<PetType> i tried different techniques i.e. collectList() but when ever i call block its throwing error stated in question , as you can see its expect PetType in return i tried Mono<PetType> but then print is causing issueHaider
Why are you blocking? Can you post a minimal example that demonstrates your problem.123
I have updated the questionHaider

1 Answers

1
votes

You are trying to implement blocking business logic in a formatter.

The purpose of the Formatter<T> interface is to write custom parsing of strings, for example json strings, csv strings etc. and parse these into an object.

What you are doing is making a database call in a formatter, which is NOT the purpose of the formatter interface.

Since you have not shown us:

  • the purpose of the formatter
  • where the formatter is used
  • whats in the passed string into the formatter
  • what your request looks like
  • What a Pet class is
  • What a PetType is

I can't help you more than this. You are trying to do a blocking call in a webflux application in an interface that does not allow reactive coding (it returns a concrete value), You need to rethink your solution to the problem.

Please explain what your problem is and what it is you want to do, and not the problem with the code, the problem you are trying to solve, and we might be able to help you more.