0

I have web application for practice. and have problem about json returning.

the unicode String which was encoded as UTF-8 are stored orcl database normally. and in the server phase this unicode String is normal state but in front-end-side(angularjs) this unicode String show as Question marks. all unicode String in front-end-side is shown as Question marks.

I have tried a lot of things now and looked up many places for this problem. I am not sure what I have to do for this.

I am using Spring 3.0.5 , Angular.js 1.2, oracle Database, tomcat 7.

This is Controller Method.

@RequestMapping(value = {"/categories"}, method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> getCategories() throws ParseException, UnsupportedEncodingException {

    List<Category> categories= postService.getCategories();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");

    if (categories == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }

    return new ResponseEntity<String>(JSONResponseUtil.getJSONString(categories), headers, HttpStatus.OK);
}

this is my applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@:1521:ORCL" />
    <property name="username" value="" />
    <property name="password" value="" />
    <property name="maxActive" value="20" />
    <property name="maxWait" value="6000" />
    <property name="poolPreparedStatements" value="true" />
    <property name="defaultAutoCommit" value="true" />
    <property name="initialSize" value="10" />
    <property name="maxIdle" value="20" />
    <property name="validationQuery" value="select sysdate from dual" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="7200000" />
</bean>


<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>


<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="typeAliasesPackage" value="acutls.model" />
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
    <property name="mapperLocations">
        <array>
            <value>classpath*:/acutls/mybatis/repository/mapper/**/*.xml</value>
        </array>
    </property>     
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

and Second Spring Config File

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="acutls" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
<context:annotation-config />
<mvc:annotation-driven/>
<mvc:resources location="/UploadedImage/" mapping="/UploadedImage/**"/>
<mvc:resources location="/resources/" mapping="/resources/**"/>

 <mvc:view-controller path="/Login" view-name="/resources/templates/login.html"/>
<mvc:view-controller path="/" view-name="/resources/templates/index.html"/> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </list>
    </property>
</bean>


</beans>

and angular.js view html file, data is stored in $scope.categories

<div class="modal fade" id="beforeWriteModal" tabindex="-1"
    role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div align="center" class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">where did you travel?</h4>
            </div>
            <div class="modal-body-beforeWriteModal">
                <input class="form-control" ng-blur="checkPostInfo()" type="text" ng-model="post.title" placeholder="enter title here" required />
                <select class="form-control" ng-model="post.category" ng-options="c.categoryId as c.categoryName for c in categories"></select>
            </div>
            <div align="left" class="modal-footer">
                <input id="postcheckbtn" class="btn btn-primary" type="button"
                    value="submit" ng-click="hideinfomodal()" />
            </div>
        </div>
    </div>
</div>

and sample data

 {
"postId": "POST14042622131875",
"realPath": "1398517998159_8.jpeg",
"title": "신혼여행",
"textContent": "",
"nickName": "Demian",
"userId": "Demian",
"categoryName": "스페인",
"categoryId": "ES"
}

and error data

{
"postId": "POST14042622131875",
"realPath": "1398517998159_8.jpeg",
"title": "????",
"textContent": "",
"nickName": "Demian",
"userId": "Demian",
"categoryName": "???",
"categoryId": "ES"
}

debug response screen capture

Demian
  • 1
  • 2

4 Answers4

2

you should set ResponseEntity header e.g.

@RequestMapping("/test22")
@ResponseBody
public ResponseEntity<String> test22(){
    MultiValueMap<String, String> map=new LinkedMultiValueMap<>();
    map.put("Content-Type", Lists.newArrayList("text/html;charset=UTF-8"));
    return new ResponseEntity<String>("you body",map , HttpStatus.OK);
}

not set header content-type will use default charset ISO-8859-1

StringHttpMessageConverter

@Override
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
    if (this.writeAcceptCharset) {
        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    }
    Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
    StreamUtils.copy(str, charset, outputMessage.getBody());
}
liaozq
  • 140
  • 1
  • 4
0

You may want to declare an encoding 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>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
0
  • First thing is to confirm if the source i.e. postService.getCategories(); is actually returning the correct formatted data
  • If the first possibility is ruled out then my assumption is that you simply need to add charset=UTF-8 in the code @RequestMapping(method=RequestMethod.GET, produces={"application/json; charset=UTF-8"})
  • If the above 2 dont work then I would suggest to use GSON
Mudassar
  • 3,135
  • 17
  • 22
  • 1. postService.getCategories() returns correct data. 2. Your suggestion needs spring version 3.2+ but i am using spring 3.0.5 3. I am using gson when i convert POJO to JSON string. – Demian Jun 18 '16 at 04:42
0

My problem was similar with yours. I used string.getBytes("UTF-8").

Please see this answer

Dongwook Kim
  • 69
  • 1
  • 10