Non-English String in Spring boot - java

I'm trying to create a String like this in Spring Boot:
model.setBody("Bạn đã nhận được một báo cáo mới");
but when I use
model.getBody().toString()
I received a weird String like this:
B?n ?ã nh?n ???c m?t báo cáo m?i
I tried it out on Java Application and it worked fines. I did some research on Google abou thow to set utf-8 and more but its still no help. Anyone know why it behave so weird like that?
P/s: I'm using
spring_boot_version=1.5.8.RELEASE
I'm using gradle

You need to Spring's CharacterEncodingFilter in your web.xml. You need to make sure this filter is the first one in the file.
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

There are multiple ways to set encoding for application.
One of the ways is set below properties in application.properties in spring-boot application.
spring.http.encoding.charset=UTF-8 # the encoding of HTTP requests/responses
spring.http.encoding.enabled=true # enable http encoding support
spring.http.encoding.force=true # force the configured encoding
For other ways see this thread

Related

Some UTF-8 characters shown as a question mark in my spring annotaded Controller

when i show this characters in the sysout in main method everything works fine and console shows characters properly. But when i put exact same sysout in my spring annotated controller it shows question marks instead. here is the code
System.out.println("əışçğ");
Please keep in mind that i just put static data in my controller for showing it. There is no any protocol or form submission that sends data. i just put above sysout code in my controller and it gives me bunch of question marks. Please help
Thanks in advance for your help
One possibility is that the characters are being displayed in different fonts. Not all fonts support all the UTF-8 characters. I ran into this with Arial recently, where Arial Unicode MS was not installed and the client defaulted to Arial.
put this in your web.xml and redeploy
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Add this in your mavn and try again after build and redeploy. Btw, which ide u r using??
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>

Get arabic word in ajax json in spring 3 issue

I have web application written in java spring 3, i have used filter
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
but when I get my json using ajax response. It shows '?????'. Is there anybody who have encountered similar issue. I have searched on internet but haven't got any solution.
If you are using mysql db on your back-end ,
Just set Collation as " utf8_general_ci " to save your arabic data, and try it again.

Tomcat 8 not responsive serving static content

When deploying our application (Java, Spring) on Tomcat 7 it is fine. Now that we upgraded to Tomcat 8 it is very slow when serving static content. Looking at developer tools (see snapshot below), each request of static content (small .js and .css files) it takes as much as we have configured for connectionTimeout in server.xml. Because default is 20000, it may take 20 secs. for each file. When dropping this to 1000 it will be faster, and take 1 sec. for each one.
This happens in different development machines using default configurations. Other processes (web services requests, etc.) are performing ok.
I wonder what and where to start looking.
This is indeed caused by an issue in the Ziplet compression filter due to a servlet spec 3.1 change (setContentLengthLong function).
I've created a pull request to fix it.
This pull request is merged into main and released on April 18th 2016 (ziplet-2.1.0)
The plugin described below (pjl-comp-filter) was used as a CompressionFilter, which turned out not to be compatible with Tomcat 8 as per an open issue in Github for ziplet (its successor) :
https://github.com/ziplet/ziplet/issues/6
I replaced it with one of these solutions and it worked :
Which compression (is GZIP the most popular) servlet filter would you suggest?
So former configuration, non working with Tomcat 8 was :
Dependency in pom.xml :
<dependency>
<groupId>org.sourceforge</groupId>
<artifactId>pjl-comp-filter</artifactId>
</dependency>
And web.xml :
<filter>
<filter-name>CompressingFilter</filter-name>
<filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
<init-param>
<param-name>includeContentTypes</param-name>
<param-value>text/html,multipart/form-data,text/css,application/x-javascript</param-value>
</init-param>
<init-param>
<param-name>compressionThreshold</param-name>
<param-value>256</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CompressingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In my case I just remove the compress filter from web.xml and everything back to normal.
The xml below don't work with tomcat 8, at least no in my application.
<filter>
<filter-name>compressionFilter</filter-name>
<filter-class>com.googlecode.webutilities.filters.CompressionFilter</filter-class>
<init-param>
<param-name>compressionThreshold</param-name>
<param-value>1024</param-value>
</init-param>
<init-param>
<param-name>ignoreURLPattern</param-name>
<param-value>.*\.(flv|mp3|mpg)</param-value>
</init-param>
<init-param>
<param-name>ignoreMimes</param-name>
<param-value>images/*,video/*, multipart/x-gzip</param-value>
</init-param>
<init-param>
<param-name>ignoreUserAgentsPattern</param-name>
<param-value>.*MSIE.*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>compressionFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>

Two separate Spring contexts for one webapp

I want to use two different Spring web contexts, each have own contextConfig, spring servlet and filter, that should be mapped to different urls. I have a
Standard Grails project, mapped to '/'
And an existing Spring webapp, that I want to map to /extra/
I know that I can deploy both into one Tomcat, but I'm looking for a way of making one app (one war, etc), because It can simplify our deployment and development process.
This applications don't need to share beans or anything, should be completely separate. Both have DispatcherServlet and DispatcherFilter (and both are using Spring Security, but different configuration)
How I can configure web.xml for such webapp?
I've tried to add new filter:
<filter>
<filter-name>extraSpringSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>extraSecurityFilterBean</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extraSpringSecurityFilterChain</filter-name>
<url-pattern>/extra/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
and spring dispatcher servlet:
<servlet>
<servlet-name>extraSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>springConfigLocation</param-name>
<param-value>classpath:extra-spring-web.xml</param-value>
</init-param>
</servlet>
Where:
two context xml in classpath (inside exra library jar):
extra-spring-web.xml
extra-spring-security.xml (!!! how I should configure it?)
extra-spring-security.xml
is pretty standard Spring Security config
have configured bean extraSecurityFilterBean
have dependecy to beans from -web context (but it's not required to be)
It's semi-working now:
as I see from logs, extraSpring servlet successfully load beans from extra-spring-web.xml
but after accessing url /extra/ I got NoSuchBeanDefinitionException: No bean named 'extraSecurityFilterBean' is defined.
So, the question, how I can define context for DelegatingFilterProxy? I even tried to add this files into main context (contextConfigLocation param), it's not what i'm looking for, but it didn't work.
I've taken a look into DelegatingFilterProxy sources, but it's not clear for me how it loads the context.
As per my comment on the question, if the security filter chain is defined in extra-spring-security.xml then you need to ensure that that file is loaded by your extra DispatcherServlet in addition to extra-spring-web.xml either by <import>ing the -security file from the -web one or configuring it as:
<servlet>
<servlet-name>extraSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:extra-spring-web.xml
classpath:extra-spring-security.xml
</param-value>
</init-param>
</servlet>
You will also need to ensure that the security filter in the Grails application doesn't apply to /extra URIs, exactly how you do this depends on whether you're using annotations, database RequestMap entries etc.
If the modules are completely separate: the easiest way is to package them as two different webapp. Tens of different spring-based apps can run in one appserver -even on a modest developer machine- without issues.
A few questions
What does your Spring Security configuration look like?
I'm confused why the error states "No bean named 'apiservSecurityFilterChain' is defined" but the web.xml you have posted only references extraSpringSecurityFilterChain (the bean names should match or some important configuration is being left out).
Possible Answer
I'm guessing the problem is that the filter-name needs to match Spring Security's bean name (cannot know for sure without seeing the Spring Security configuration you are using). The default value used by the Spring Security namespace is springSecurityFilterChain, so try the following in the web.xml instead (notice extraSpringSecurityFilterChain changed to springSecurityFilterChain):
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>extraSecurityFilterBean</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/extra/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

Send specific headers on MIME-type response in Apache Tomcat

I have an Apache Tomcat server running. If I have something like:
webapp/
image1.png
Then, I can simply access it using:
example.com/image1.png
Which is perfect, except that I don't have any control over what headers are being sent. I wish to send specific Expires header for certain MIME-types (like, for image/png). These headers will be static, so I don't really mind if I have to specify this in some XML file and cannot be dynamic.
Is it possible with Apache Tomcat? The other obvious way is to read from the file and output it to the browser with the appropriate headers, but I think that it might be an overkill.
Use Tomcat Filters for applying this headers.
<web-app ...>
...
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType text/css</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/javascript</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
</filter>
...
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
...
</web-app>
More Info at Tomcat Filter Documentation
It's possible that you're not using Tomcat 7, but an older version. In my Tomcat 7 installation, I found that filter packaged up in catalina.jar

Categories