Get arabic word in ajax json in spring 3 issue - java

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.

Related

Non-English String in Spring boot

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

java rest service through forbidden 403 error

I'm using java to build web api using rest service.
I have add is filter to my web.xml but post request return forbidden 403 error
but when I use get request it works fine.
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>POST,GET,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>false</param-value>
</init-param>
</filter>
Here's postman view
When POST is used, the intention from client is to create a resource.
For example,
POST /api/apples/
BODY {color: "green"}
Client issuing this with the intention to create a create apple object at the server side.
GET in other hand is retrieve an object/objects from the server.
For example,
GET /api/apples/
It returns all apples.
If the client posts to an URL and gets 403, it means that your application does not have permission to create object in the server.
add below dependency in your project it's because of the version mismatch of the httpclient. try using version 4.5.3
maven repo link

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>

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>

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