1

Is there a way I can write a generic controller advice to replace all control characters like \n in the response of a Spring Boot controller? Currently, the return type of the controller is of type ResponseEntity<T>. There will be multiple attributes within the POJO that needs this string replacement. I am trying to avoid doing a string replace on each and every attribute of the POJO.

Halley
  • 521
  • 10
  • 35
  • I guess you could experiment with a custom implementation of `HttpMessageConverter` if you don't need the `\n` context at all (I doubt). Why do you need it after all? – terrorrussia-keeps-killing Oct 06 '21 at 15:04
  • 1
    Must it be a "controller advice"??.... assuming spring-boot defaults (jackson-json): https://stackoverflow.com/q/6852213/592355 this could help/duplicates. – xerx593 Oct 06 '21 at 15:05
  • @fluffy the UI client lacks capability to evaluate \n. They need \\n and \\t and on on. – Halley Oct 06 '21 at 15:40
  • @Halley I don't really understand what you mean by "evaluating `\n`". If it's a part of the domain logic (at least it sounds so), why not emit properly escaped strings in your services for any `T` rather than modifying the output? – terrorrussia-keeps-killing Oct 06 '21 at 15:43
  • UI client cannot create a new line with just `\n`. When the data get stored in the database, all the new lines are getting encoded as `\n`. This needs to be done only to certain controllers which have apis which are consumed by those clients. – Halley Oct 06 '21 at 15:47
  • 1
    @xerx593 The question you linked has the necessary answers I need. I believe we can mark this as duplicate. Cheers – Halley Oct 06 '21 at 15:47

0 Answers0