I am using grails framework to develop a web application. I need to generate the runtime server URL as follows: http:hostname:port/application/. In grails configuration we have an option to provide the serverURL using grails.serverURL.
My question is: Can we get the port number of server which the application is running at runtime? Is this possible?
Try this..
System.getProperty("server.port", "8080")
Enjoy...
If you have different profiles(config blocks) per environment, you can likely set the grails.server.port.http property in there.
Related
I am trying to create a Declarative client to test the API I am writing in grails 4, like you can do it in micronaut.
As far as I have come, you can create a declarative client if you know the base URL at compile time. Because the grails functional (integration) tests create a server on a random port, you cannot know that port beforehand and use the #Client('http://localhost:8080') annotation.
Using something like #Client('http://localhost:${local.server.port}') fails with grails complaining that the configuration property cannot be looked up.
Is there something I am missing?
Is it possible to use Micronnaut Declarative HTTP client in grails 4
functional API tests?
It is.
A problem with doing that is that when using #Client('/') to connect to the local running server, an EmbeddedServer bean needs to be configured in the context to answer the question of what port the app is running on. Some of the relevant code is at https://github.com/micronaut-projects/micronaut-core/blob/458bbf07221407abe7e283815fb62b7b4d1fc224/http-client-core/src/main/java/io/micronaut/http/client/DefaultLoadBalancerResolver.java#L93.
We will add testing support to make this easier but you can make it work by registering the bean yourself. See the following:
src/integration-test/groovy/com/objectcomputing/example/TestEmbeddedServer.groovy
src/integration-test/groovy/com/objectcomputing/example/BookClient.groovy
src/integration-test/groovy/com/objectcomputing/example/EmbeddedServerTest.groovy
src/integration-test/groovy/com/objectcomputing/example/BookFunctionalSpec.groovy
That is a kludge for now, but I believe it will work until proper support is released in the testing framework.
I have a grails web application with a domain model and a hibernate datasource persistence.
I would like to write now a command line tool in groovy to access also this domain model and the hibernate datasource.
Any ideas how I can do this?
regards
Vanigor
You can use Spring Boot as described in this article.
That example creates a small web UI, but Boot can be configured as a CLI app.
I have a grails controller which handle fileupload.
I deployed the application on glassfish in windows and in ubuntu.
On windows deployment, the request object of
request.getFile("filename")
is of type commons.CommonsMultipartFile
while on ubuntu deployment the request is of type support.DefaultMultipart
What causes this different behavour?
Is there a way to make it consistent?
The application server is glassfish3.1.2
Anything grails supplies via the request.getFile(...) method should implement the Spring MultipartFile interface. As long as you stick to the methods available in the interface, you shouldn't need to worry about what concrete implementation you receive.
the problem solved after I upgraded the glassfish from 3.1.2 to glassfish 4 in windows and ubuntu.
there may be some other ways to solve this. but i solved this by upgrading the glassfish.
We've developed a Grails application that uses redirects.
Beacuse of external reasons, we are just recently using reverse-proxy, to split some traffic to domains:
From:
demo1.company.local (the server itself)
To:
tomcat.company.local (for all java applications, including our grails app)
lotus.company.local (for all Domino applications)
Since tomcat is only configured in the hosts file on the demo1 server, the redirects do not work when I access the application from anywhere else then the demo1 server itself.
I've tried to solve this using "absolute" and/or "base" parameter in Grails' redirect(), but if I understand correctly, this is Grails 2+ only and we're using Grails 1.3.4.
Are there other ways to redirect to a specified host?
Am I misusing things?
Thanks,
Bram
If you define grails.serverURL in Config.groovy, redirects with absolute:true will use that value for the URL.
Grails 2.0 changed with the way it uses grails.serverURL for development and test environments (as described in the manual). However, I've had several problems with serverURL in regard to production deployment on Tomcat. There seem to be several options for serverURL (production mode):
(Added) this setting is just "smoke and mirrors", because Tomcat and Jetty have methods to control how the URL is handled for the App, down to the "App" level.
Use it to specify the server (as is pointed out as a "TODO" in Config.groovy)
Don't use it as indicated here by one of the Grails contributors, i.e. "It should always be safe to remove the serverURL property and let Grails generate urls relative to the current running app." It's not clear if this extends to production or not (when not generating emails).
Use another method instead, namely grails.app.context, which is not documented in Grails 2.0 manual, but is described in certain references, here and here.
Can you please clarify the correct use of serverURL and app.context, both for Jetty and Tomcat (production mode)?
Thanks
Good question! I was just looking for the correct way to get the actual serverURL without explicitly needing to configure it in Config.groovy (in a Grails 2.1 application).
Since Grails 2.0, there is a super-useful class called LinkGenerator that you can use virtually anywhere, for example in a Service:
import org.codehaus.groovy.grails.web.mapping.LinkGenerator
Class MyService {
LinkGenerator grailsLinkGenerator
String serverUrl() {
// Generate: http://localhost:8080/link-generator
grailsLinkGenerator.serverBaseURL
}
}
Thanks to Mr. Haki for blogging about this!
So the basic idea of the grails.serverURL configuration parameter is to allow the createLink method to know what URL you want when creating absolute links. If no grails.serverURL configuration parameter is specified, it will default to http://localhost:8080 (unless server.port is specified, then 8080 will be whatever) ...
The application context tells Jetty/Tomcat to run the application in a different root. For example, specifying
grails.app.context="/myApp"
will set your application root to "/myApp". In production mode, the application context is handled by the web container and this directive is ignored. The choice to configure your production jetty or tomcat instances to run your application in a different context is entirely dependent on what your technical requirements are.
So the real thing to consider is, in your application are you creating a lot of absolute links in your GSPs where you need to define a "production" serverURL? If not, then you don't need to specify it; if you are, then you'll need to specify it.
As a personal preference, the first thing that I always do after creating a new grails project is go into the config and change the grails.app.context to "/" ... It makes mirroring a production environment much easier for me.
Hope this clears things up!