Grails JSONBuilder

I finally figured out how to do this using a JSONBuilder, here’s the code import grails.web.* class JSONSerializer { def target String getJSON() { Closure jsonFormat = { object = { // Set the delegate of buildJSON to ensure that missing methods called thereby are routed to the JSONBuilder buildJSON.delegate = delegate buildJSON(target) } } … Read more

Grails Date unmarshalling

The cleanest way is probably to register a custom DataBinder for possible date formats. import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CustomDateBinder extends PropertyEditorSupport { private final List<String> formats; public CustomDateBinder(List formats) { List<String> formatList = new ArrayList<String>(formats.size()); for (Object format : formats) { formatList.add(format.toString()); // Force String … Read more

Grails get child domain objects

The issue is with the use of default JSON converter. Here are your options: 1. Default – all fields, shallow associations a. render blah as JSON 2. Global deep converter – change all JSON converters to use deep association traversal a. grails.converters.json.default.deep = true 3. Named config marshaller using provided or custom converters a. JSON.createNamedConfig(‘deep’){ … Read more

How to access Grails configuration in Grails 2.0?

If you need it in an artifact that supports dependency injection, simply inject grailsApplication class MyController { def grailsApplication def myAction = { def bar = grailsApplication.config.my.property } } If you need it in a bean in, say, src/groovy or src/java, wire it up using conf/spring/resources.groovy // src/groovy/com/example/MyBean.groovy class MyBean { def grailsApplication def foo() … Read more

Grails: displaying created image in gsp

If you write the bytes to the output stream, you can treat the controller/action as the source of the image in your GSP. Here’s a quick, untested example: // controller action def displayGraph = { def img // byte array //… response.setHeader(‘Content-length’, img.length) response.contentType=”image/png” // or the appropriate image content type response.outputStream << img response.outputStream.flush() … Read more

Grails 2.4 and hibernate4 errors with run-app

It’s a bug, it seems that you can leave it that way and will cause no problem, but if you don’t want to see the message here are some solutions: (Edit: Option 2 seems to work better (see comments in this post)) 1.- singleSession configuration from DataSource.groovy https://jira.grails.org/browse/GRAILS-11198 2.- overriding the H2 dialect: public class … Read more

Copy entire directory contents to another directory? [duplicate]

FileUtils.copyDirectory() Copies a whole directory to a new location preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory. The destination directory is created if it does not exist. If the destination directory … Read more

Grails3 file upload maxFileSize limit

Grails 3, Grails 4 and Grails 5 The solution is to set maxFileSize and maxRequestSize limits inside your application.yml file (usually placed inside grails-app/conf/). Be sure to have something like this: grails: controllers: upload: maxFileSize: 2000000 maxRequestSize: 2000000 Replace 2000000 with the number of max bytes you want for a file upload and for the … Read more

FileUpload with JAX-RS

On Server Side you can use something like this @POST @Path(“/fileupload”) //Your Path or URL to call this service @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @DefaultValue(“true”) @FormDataParam(“enabled”) boolean enabled, @FormDataParam(“file”) InputStream uploadedInputStream, @FormDataParam(“file”) FormDataContentDisposition fileDetail) { //Your local disk path where you want to store the file String uploadedFileLocation = “D://uploadedFiles/” + fileDetail.getFileName(); System.out.println(uploadedFileLocation); // save it … Read more