1

In Grails this could be use for content negotiation, especially useful to implement APIs:

withFormat {
    xml { ... some code that renders XML }
    json { ... some code that renders JSON }
}

Now, if I need a default format, let's say JSON, the "... some code that renders JSON" should be executed twice, once for the JSON option and once for the "*" option that is, AFAIK the "any other matching format", which is a way to specify the default format, like:

withFormat {
    xml { ... some code that renders XML }
    json { ... some code that renders JSON }
    "*" { ... some code that renders JSON }
}

My questions are:

  1. Is that the correct way of specifying the default format as JSON?

  2. Is there a way of not repeating the same code for two options? (I mean something like: json, "*" { ... }

Pablo Pazos
  • 3,080
  • 29
  • 42

2 Answers2

1

Instead of this...

withFormat {
    xml { ... some code that renders XML }
    json { ... some code that renders JSON }
    "*" { ... some code that renders JSON }
}

Use this:

withFormat {
    xml { ... some code that renders XML }
    "*" { ... some code that renders JSON }
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • This is one of those moments wen I realize how blind I was by thinking I need to explicitly put each format then the "rest", so simple... thanks Jeff, you are always a great help here. – Pablo Pazos Dec 29 '20 at 16:34
-1

The one problem with this is that it is highly redundant and would have to be done in EVERY CONTROLLER METHOD. Would suggest moving this functionality to HandlerInterceptor or using something like Beapi API Framework plugin

Orubel
  • 316
  • 4
  • 16
  • Thanks for the comment but doesn't answer the question. – Pablo Pazos Dec 29 '20 at 16:32
  • You didnt try the framework. It auto formats based on the API request. If you call in JSON, it autoformats for JSON, etc. You dont have to bother REDUNDANTLY CODING THIS IN EVERY SINGLE CONTROLLER METHOD. Though I'm sure there are some Grails devs at OCI who think bad OOP is good practice. – Orubel Dec 29 '20 at 17:19
  • The question is about how to do that in Grails, which I think it's possible, since it was answered correctly by Jeff, so no framework or plugin is needed. Either way, the answer doesn't say HOW to do what was asked, that is why this answer is not correct and lacks context. – Pablo Pazos Dec 29 '20 at 18:39
  • The functionality you are using to do that IS a plugin. Grails 3 detaches functionality into plugins. So yes... you ARE using a plugin, several in fact. Look at your build.gradle file. The Beapi Framework merely obfuscates, simplifies and automates so mistakes like this don't occur. And Jeff answered to the best of his ability with his knowledge of API's; this does not make it the best answer. Just the AVAILABLE answer. – Orubel Dec 30 '20 at 16:53
  • I know that is a plugin, there is no need of pushing... that plugin comes out of the box... if you didn't notice yet: I don't want to add anything extra. Thanks for the comment, the best answer was chosen, and that is chosen by the person who created the question, that is me. Thank you again, please stop arguing. – Pablo Pazos Jan 06 '21 at 03:25
  • I accept that and am not arguing. Was pointing out that the functionality you are trying to implement is NOT 'out of the box' (as you state). But best of luck. – Orubel Jan 07 '21 at 04:27
  • As said: current selected answer fits exactly my needs out of the box. – Pablo Pazos Jan 07 '21 at 06:28