Accessing the application.conf properties from java class with Play! 2.0 - java

I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file.
I don't want to hard code the file path in the source, and so I want to get that path from the application.conf.
The problem is that I don't know how to access these properties from the java class.
I tried this:
Configuration.root().getString("file.path")
But it ends with a NullPointerException.
Am I wrong in assuming that there's a global Configuration instance that I can use?
Thanks.

Try Play.application().configuration().getString("your.key")
As noted in the comment (nico_ekito), please use play.Play and not play.api.Play. play.api.Play is for scala controllers (see comment by Marcus biesior Biesioroff)
Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.

Even if it seems simple, here is the scala way to get properties from configuration file :
Play 2.0 and 2.1 :
import play.api.Play.current
...
Play.application.configuration.getString("your.key")
Play 2.2 and +
import play.api.Play.current
...
current.configuration.getString("your.key")
Using Typesafe config
import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");

From Play 2.4 and + it is better to use dependency injection to access Configurations:
import play.Configuration;
import javax.inject.Inject;
#Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");

Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
ConfigFactory.load().getString("my.var");

In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")

Use as following (Tested in Play 1.2.5)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file

As a reference to access it from the template (for play < 2)
play.configuration['your.key']

As folks have mentioned, Play.application.configuration no longer exists.
In Play Scala 2.3.x, to read a value from conf/application.conf, you can do the following:
import play.api.Play.current
...
current.configuration.getString("key")

In Play 1.2.x
import play.Play;
...
String version = Play.configuration.getProperty("application.version.number", "1.1.1");
where the second parameter is the default value

Import this
import com.typesafe.config.Config;
and write the below lines
private Config config;
this.config = ConfigProvider.config();
String value = this.config.getString("fieldFromConfigFile");

import play.Play;
String myVal = Play.configuration.getProperty("your.key").toString();
i use this in my app and it works
Dont forget to import play.Play. Hope it'll gives you help

Starting from version 2.5 please use play.Application class which should be injected and then
application.config().getString("your.property.here")

For Java Playframework:
In Application.conf, you can put something like that:
email="example#gmail.com.pe"
some class:
import play.Play;
String email = Play.application().configuration().getString("key") // key ->email

Related

Openapi generator: Add import class to generated model

I am using Openapi generator(5.4.0), with spring (generator name) and gradle, I am trying to add an import to a generated model.
For the particular field in the api spec, I have added:
x-field-extra-annotation: "#com.fasterxml.jackson.annotation.JsonFormat ...."
This works, however I dont want to fully qualify it, and have the com.fasterxml.jackson.annotation.JsonFormat import added.
I tried adding typeMappings to genratedCode task, but that doesn't work.
importMappings = [
'JsonFormat' : 'com.fasterxml.jackson.annotation.JsonFormat'
]
Update:
I can add model.mustache template to project, and add the import. ie
{{#useBeanValidation}}
...
import com.fasterxml.jackson.annotation.JsonFormat;
...
{{/useBeanValidation}}
Any ideas? Better ways.
Thanks.
There is no production-ready solution (yet!)
If you are not starving for this feature, wait for acceptance of related issue and PR to one of the nearest releases.
https://github.com/OpenAPITools/openapi-generator/issues/13938
It will allow required customizations using
x-spring-constraint: #MyAnnotation(value = 1, message = "nice") (or x-java-constraint) extension with configOption
customValidationAnnotationsPackages="my.custom.package.MyAnnotaion;my.other.package.*"
Else, refer to https://bartko-mat.medium.com/openapi-generator-to-spring-boot-with-custom-java-validations-623381df9215 as tutorial for handmade solution

What class should I use instead of the deprecated AmazonCloudFrontClient?

I am trying to invalidate items in cloud front. I fount that the class http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudfront_2012_03_15/AmazonCloudFrontClient.html is deprecated.
What class should I use?
There is a class with the same name in another package which is not #Deprecated:
Docs for com.amazonaws.services.cloudfront.AmazonCloudFrontClient
The deprecated class is in the namespace com.amazonaws.services.cloudfront_2012_03_15.AmazonCloudFrontClient.
I realized my IDE was importing from the wrong package.
Use this:
import com.amazonaws.services.cloudfront.AmazonCloudFrontClient;
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudfront/AmazonCloudFrontClient.html#createInvalidation(com.amazonaws.services.cloudfront.model.CreateInvalidationRequest

cannot resolved to a type error in java

I'm new in java, please help me to understand this.
I can see there is ReadHtml class and defined with one public method. But when i put this code in ecplise, it shows red mark under WebClient with tag that "this cannot resolved to a type". May I know what does it mean? Gone through all about method definition but couldn't find any remedy to understand this.
Can I get any help ?
public class ReadHtml {
public static LinkedList<String> readJacksonCounty(String urlName, String pStartDate,String pFinishDate)
{
LinkedList<String> xmlListReturn=new LinkedList<String>();
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");
final WebClient webClient1 = new WebClient(BrowserVersion.CHROME);
webClient1.setJavaScriptTimeout(60000);
webClient1.getCookieManager().setCookiesEnabled(true);//enable cookies
webClient1.getCache().clear();
You are missing an import of this library:
import com.gargoylesoftware.htmlunit.WebClient;
Add this to the top of your file (and read dsp_user's comment for future reference).
Basically "...cannot be resolved to a type" means that type is not available on the class path. If you're just using eclipse refere to How to import a jar in Eclipse.
If you already added the needed jar onto your class path, you are missing the import statement. Imports just make it so that you dont have to use a class's fully qualified name. (you can type
MyClass myClass;
as opposed to
com.some.package.MyClass myClass;
if you add
import com.some.package.MyClass;
at the top of your file.
Note that if you want to build a jar from your project you'll need some kind of build tool. If you choose to use Maven, which is very common, just read any tutorial on how to get started and manage dependencies.

Running independent sub-projects on one Play server doesn't work

I'am trying to run multiple independent Play Framework 2.2 projects on one single Play server.
Problem is that when I use run or start to start the application, then the "sub"-project shows the content of the "root"-project. I have read everything avaliable, but I can't figure out a solution :/
Here's the build.sbt:
lazy val aaaroot = project.in(file(".")).aggregate(project1).dependsOn(project1)
lazy val project1 = project.in(file("project1"))
The routes file of the "root"-project:
GET / controllers.Application.index()
GET /assets/*file controllers.Assets.at(path="/public", file)
-> /project1 project1.Routes
The routes file (called project1.routes) of the "sub"-project:
GET / controllers.project1.Application.index()
GET /assets/*file controllers.project1.Assets.at(path="/public", file)
The Application.java Controller of the subproject uses package controllers.project1;
I also tried to set up the "Router object" in the application.conf file of the "sub"-project ( application.router=project1.Routes ). As described here.
Every suggestion is highly appreciated!!!
Found the error. The controller is used correctly, but there is a name-conflict with the views.
My Application.java says:
package controllers.project1;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index() {
return ok(index.render("Project1"));
}
}
The error is the line import views.html.*;. This calls the views from the main-project. To avoid it I had to rename the view-folder to something else and edit the import like import viewsofproject1.html.*;

JavascriptInterface annotation for JELLY_BEAN and below

As clearly noted on official docs, usage of #JavascriptInterface is needed for API level JELLY_BEAN_MR1 and above, to access a java function from the webview side.
This means that Project Build Target must point to API 17 or above which resolves the following import:
import android.webkit.JavascriptInterface;
How does android handles this code for API 16 and below? Will I get a runtime exception or does it ignore this import on runtime?
I'm quite surprised with these answers... they are not accurate.
If you add the JavascriptInterface and another annotation lets say MyAnnotation to the same method(like I did), and then try to access the MyAnnotation instance annotation then you are in for a ClassDefNotFoundException surprise!
My solution which seems to work for now (it has been more than a year), is to add the annotation declaration to the application project:
package android.webkit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD})
public #interface JavascriptInterface {}
This solved the problem on 2.3.5, and still worked on 4.3 and 4.4 and 4.2.
Hope this helps someone else!
The annotation class JavascriptInterface is not loaded by older Android versions, so your code will run without any issue on older versions.
Imports like the one you have suggested import android.webkit.JavascriptInterface; are only hints to the compiler so it can resolve the full class path you are referring to further on in your source. The VM will only throw a ClassNotFoundException when you try and use the class. So the simple answer is no, everything will be fine as long as you protect your use of the JavascriptInterface with something like the following.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
JavascriptInterface js = new JavascriptInterface(){ ... };
}

Categories

Resources