How can I "say" Spring Boot to use the UTF-8 encoding, to show and save German umlauts correctly?
We are programming a Java-Webapplication using Sping-Boot 1.1.1 (Release) and as webserver a TomCat7 or Jetty. The database is postgresql or h2 for testing.
Edit:
I tried it with the properties file (thanks for the answer), but no changes are visible.
The database is also UTF-8...
Especially the problem comes, when we send a POST-Request to the Webserver.
The Spring-Request-Handler gets already the broken encoded values.
In the following you can see a part of the code:
(It shows a snippet of the Thymeleaf-Template)
<form accept-charset="utf-8" method="post">
<div class="row">
<fieldset th:object="${model}">
<!-- CSRF token -->
<th:block th:replace="makros :: csrf" />
<div class="col-sm-4 form-group" >
<label for="firstname" th:text="#{edit_user.first_name}">Given Name</label>
<input class="form-control required" type="text" required="required" id="firstname" name="firstname" th:field="*{firstName}" />
</div>
<div class="col-sm-4 form-group">
<label for="firstname" th:text="#{edit_user.last_name}">Family Name</label>
<input class="form-control required" type="text" required="required" id="lastname" name="lastname" th:field="*{lastName}" />
</div>
</fieldset>
</div>
</form>
And this is the request handler for that:
#RequestMapping(method = RequestMethod.POST)
public String handleUserUpdate(#ModelAttribute(MODEL) UpdateUserCommand command) {
//here we cut the broken encoded values
}
Greetings
Stef
This helped:
spring.datasource.url=jdbc:mysql://localhost:3306/securitydb?useUnicode=yes&characterEncoding=UTF-8
What is wrongly encoded? The request or the response? server.tomcat.uri-encoding is switching the URI decoding to UTF-8 (this is already the case for Jetty).
But that does not do anything for the request body. By default, Spring MVC decodes that with ISO-8859-1 (that is the default per the servlet spec). You need to specify a body encoding in your request if you want it to be decoded using UTF-8. Most users are actually using the CharacterEncodingFilter to achieve the same thing (and ensure consistency).
If that fixes your issue, watch out for #1182 that is meant to provide an auto-configuration for that.
The latest should be UTF-8 by default I think? See docs on server.tomcat.uri-encoding: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties. OTOH it might depend on where you need the encoding to happen (Spring Boot knows nothing about your database server encoding for instance).
You can set UTF-8 as default encoding for spring-boot-maven-plugin.
I create 2 maven profile to run spring boot in UTF-8 and set active spring profile via maven profile:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF8 -Dspring.profiles.active="dev"</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
And run
mvn spring-boot:run for dev profile (application-dev.properties) and
mvn -P prod spring-boot:run for application.properties
Related
I have cucumber selenium automatin java project with:
I am using cucumber 4.8.1, and maven-cucumber-reporting 4.3.0
I am using mvn clean install in my local machine to execute the test
When I have an error, the cucumber.json file includes the screenshot:
"after": [
{
"embeddings": [
{
"data": "iVBORw0KGgoAA..."
"mime_type": "image/png"
}
],
And in the output folder the embeddings folder is created and includes the screenshot correctly
This is my plugin configuration:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>4.3.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>cucumber-jvm-example</projectName>
<!-- optional, per documentation set this to "true" to bypass generation
of Cucumber Reports entirely, defaults to false if not specified -->
<skip>false</skip>
<!-- output directory for the generated report -->
<outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory>
<!-- optional, defaults to outputDirectory if not specified -->
<inputDirectory>${project.build.directory}/cucumber-reports</inputDirectory>
<jsonFiles>
<!-- supports wildcard or name pattern -->
<param>**/cucumber.json</param>
</jsonFiles>
<!-- optional, defaults to outputDirectory if not specified -->
<classificationDirectory>target/cucumber-reports/advanced-reports</classificationDirectory>
<classificationFiles>
<param>sample.properties</param>
<param>other.properties</param>
</classificationFiles>
<!-- optional, set true to fail build on test failures -->
<checkBuildResult>false</checkBuildResult>
</configuration>
</execution>
</executions>
</plugin>
But, in the overview-features.html file when I open the error in the step, after the error the link to display the screenshot is not displayed.
Do you know what is missing or wrong?
Many thanks
Xavier
SOLVED !!!!
Now the link to the screenshot is inside the Hooks Section
In the previous versions (I was working with cucumber 1.2.5 and maven-cucumber-reporting 4.3.0 the link "Attachment.1 (png) was in the steps section, not in the Hooks.
After several days I have found the solution. The ticket can be closed!!!
I hope this can help other people with the same problem!!!
Using SOA suite 11, trying building source code (composite.xml with configuration file) in SOA composite application into a jar file using maven.
Can anyone help to guide me making POM.xml for the same. i am using "Apache-ANT-Plugin" in my pom file.
http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This POM relates to this SOA Composite, i.e. the one in this same directory.
There is another POM in the SOA Application directory which handles
the whole SOA Application, which may contain additional projects.
-->
4.0.0
test1
HelloWrold
1.0-SNAPSHOT
sar
<!--
The parent points to the common SOA parent POM. That is a special POM that is
shipped by Oracle as a point of customization (only). You can add default values
for properties like serverUrl, etc. to the SOA common parent POM, so that you
do not have to specify them over and over in every project POM.
-->
<parent>
<groupId>com.oracle.soa</groupId>
<artifactId>sar-common</artifactId>
<version>12.1.3-0-0</version>
</parent>
<properties>
<!-- These parameters are used by the compile goal -->
<scac.input.dir>${project.basedir}\SOA/</scac.input.dir>
<scac.output.dir>${project.basedir}/target</scac.output.dir>
<scac.input>${scac.input.dir}/composite.xml</scac.input>
<scac.output>${scac.output.dir}/out.xml</scac.output>
<scac.error>${scac.output.dir}/error.txt</scac.error>
<scac.displayLevel>1</scac.displayLevel>
<!-- if you are using a config plan, uncomment the following line and update to point
to your config plan -->
<!--<configplan>${scac.input.dir}/configplan.xml</configplan>-->
<!-- These parameters are used by the deploy and undeploy goals -->
<composite.name>${project.artifactId}</composite.name>
<composite.revision>1.0</composite.revision>
<composite.partition>default</composite.partition>
<serverUrl>${oracleServerUrl}</serverUrl>
<user>${oracleUsername}</user>
<password>${oraclePassword}</password>
<overwrite>true</overwrite>
<forceDefault>true</forceDefault>
<regenerateRulebase>false</regenerateRulebase>
<keepInstancesOnRedeploy>false</keepInstancesOnRedeploy>
<!-- These parameters are used by the test goal
if you are using the sca-test (test) goal, you need to uncomment the following
line and point it to your jndi.properties file. -->
<!--<jndi.properties.input>UNDEFINED</jndi.properties.input>-->
<scatest.result>${scac.output.dir}/testResult</scatest.result>
<!-- input is the name of the composite to run test suties against -->
<input>${project.artifactId}</input>
<!--<scac.ant.buildfile>${env.MW_HOME}/soa/bin/ant-sca-compile.xml</scac.ant.buildfile>
<sca.ant.testfile>${env.MW_HOME}/soa/bin/ant-sca-test.xml</sca.ant.testfile>
-->
</properties>
<build>
<plugins>
<plugin>
<groupId>com.oracle.soa.plugin</groupId>
<artifactId>oracle-soa-plugin</artifactId>
<version>12.1.3-0-0</version>
<configuration>
<compositeName>${project.artifactId}</compositeName>
<composite>${scac.input}</composite>
<sarLocation>${scac.output.dir}/sca_${project.artifactId}_rev${composite.revision}.jar</sarLocation>
<serverUrl>${serverUrl}</serverUrl>
<user>${user}</user>
<password>${password}</password>
<!-- Note: compositeRevision is needed to package, revision is needed to undeploy -->
<compositeRevision>${composite.revision}</compositeRevision>
<revision>${composite.revision}</revision>
<scacInputDir>${scac.input.dir}</scacInputDir>
<input>${input}</input>
</configuration>
<!-- extensions=true is needed to use the custom sar packaging type -->
<extensions>true</extensions>
</plugin>
</plugins>
</build>
I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :
<h4 th:text="${version}">Version</h4>
The data is well set in the pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.test.navig</groupId>
<artifactId>navigo</artifactId>
<version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :
Implementation-Version: 2.0.3-SNAPSHOT
I've tried to get the appplication version in the controller and set it in a ModuleAttribute :
#ModelAttribute("version")
public String getVersion() {
logger.info("ModelAttribute to get application version");
return getClass().getPackage().getImplementationVersion();
}
But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.
I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.
1. If your pom.xml use spring-boot-starter-parent as parent, you can use #project.version# to get version (and any other Maven properties) in your application.properties file. According to Spring docs:
You can automatically expand properties from the Maven project using
resource filtering. If you use the spring-boot-starter-parent you
can
then refer to your Maven ‘project properties’ via #..# placeholders
Maven pom.xml:
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Spring application.properties:
foo.app.version=#project.version#
2. Then a class annotated with #ControllerAdvice can be used to inject version as model attribute.
#ControllerAdvice
public class ControllerAdvice {
#Value("${foo.app.version}")
private String applicationVersion;
#ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
3. Finally this model attribute can be accessed by Thymeleaf as any other.
<th:block th:text="${applicationVersion}"></th:block>
Hope this helps!
Here is the simplest way I've found :
In my controller :
#ModelAttribute("version")
public String getVersion() throws IOException {
logger.info("ModelAttribute to get application version");
Manifest manif = new Manifest(
Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = (String) manif.getMainAttributes().get(
Attributes.Name.IMPLEMENTATION_VERSION);
return version;
}
In my htm page :
<h4 th:text="${version}">Version</h4>
You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.
In the generated war, the version (in fact ${project.version}) will be hardcoded to your POM version.
I'm facing problems with a jdbc dynamic properties configurer. I try to explain what exactly the problem is.
When I do mvn clean install and right after I deploy the applications in my server (Weblogic 10.3.3), everything is correct, and all the applications work fine. But, every morning, when I try to redeploy the same applications, it was shown an error message like this:
Error creating bean with name 'path.to.my.bean.JDBCPropertiesFactoryBean#6015a10' defined in class path resource [spring/configuration/placeholder-jdbcproperties.xml]: Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
This application.version comes from maven pom.xml:
<properties>
...
<application.version>MyVersion</application.version>
...
</properties>
The bean is:
<bean id="jdbcPlaceholderConfig"
class="path.to.my.bean.DefaultPropertyPlaceholderConfigurer"> <!-- Class to extend PropertyPlaceholderConfigurer -->
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="properties">
<bean class="path.to.my.bean.JDBCPropertiesFactoryBean"> <!-- Class to extend PropertiesFactoryBean -->
<property name="query">
<value>
SELECT
A.COLUMN1 || '.' || P.COLUMN2,
COLUMN3
FROM
T_TABLE_WITH_PROPERTIES${application.version} P,
T_TABLE_WITH_PROPERTIES_2 G
WHERE G.ID = P.ID
</value>
</property>
<property name="dataSource" ref="ref.to.datasource.bean"/>
</bean>
</property>
So, every morning I have to rebuild with maven, and the loop starts again.
Additional information: I try to use JRebel too, but I'm not sure where can be the problem, maybe this is relevant.
Thanks in advance.
UPDATE:
This how I generate the rebel.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've just realized that with <executions>...<goal>generate</goal>...</executions>, when I do mvn clean install, without jrebel:generate, the rebel.xml files are always generated, so maybe I have to delete the executions tag, and generate the rebel.xml files once with jrebel:generate, and then, edit the rebel.xml and do again mvn clean install.
Would be that correct?
Thanks.
UPDATE WITH THE SOLUTION:
This is the final version of maven jrebel plugin in the pom.xml:
<build>
...
<plugins>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.5</version>
<configuration>
<relativePath>../../</relativePath>
<rootPath>PATH\TO\MY\SIS_VOB</rootPath>
<addResourcesDirToRebelXml>true</addResourcesDirToRebelXml>
<alwaysGenerate>true</alwaysGenerate>
</configuration>
<!-- executions tag out! to not regenerate files always -->
</plugin>
</plugins>
</build>
To create the rebel.xml:
mvn jrebel:generate
Then, if we want, we can modify the rebel.xml files if we want to exclude some files, like *.properties, as Henri's answer.
And that's it!
This can happen if you're using resource filtering with JRebel, as the application looks up the bean's xml in its unfiltered form from the project working directory (as per rebel.xml).
To resolve this, you'll need to update rebel.xml for that module, adding exclude for that particular XML file - see here.
Example
I need to use the value of "config.enableInstitutions" property in pom.xml to be used in Fremarker template as the value of a hidden input.
Pom.xml
<properties>
<config.enableInstitutions>true</config.enableInstitutions>
</properties>
myfreemarker.ftl
<body>
<input type="hidden" value="<< value of config.enableInstitutions property here >>"/>
How do I use that property in freemarker template?