質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

Q&A

1回答

29956閲覧

Spring4:ResponseBodyで返した日本語が文字化けする

Zaganchan

総合スコア80

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

0グッド

1クリップ

投稿2016/04/14 08:15

編集2022/01/12 10:55

###発生している問題・エラーメッセージ

Ajax通信でSpringから返した値のうち、日本語が全て半角の「?」に置き換わってしまいます。
?は文字数に対応しているため、マルチバイトのみ全て?になっていると思われます。

###該当のソースコード
Spring

@RequestMapping(value = "/test", method = RequestMethod.POST, headers = {"Content-type=application/json;charset=UTF-8,application/xml;charset=UTF-8,text/plain;charset=UTF-8"},consumes = {"application/json; charset=UTF-8,text/plain;charset=UTF-8"}) @ResponseBody public String child_kensaku(@RequestBody String inputdata , HttpServletRequest request, HttpServletResponse response, HttpSession session, SessionStatus sessionstatus ) throws IllegalArgumentException, IllegalAccessException, ParseException, JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); String ret = mapper.writeValueAsString(jsonstr); //jsonstrに日本語のデータがある return ret; }

JS

$(function(){ $("#ajax_btn").click(function ajaxpost() { // 多重送信を防ぐため通信完了までボタンをdisableにする var button = $(this); button.attr("disabled", true); // 各フィールドから値を取得してJSONデータを作成 var data = { 'testdata': 'testdata', beforeSend: function(xhr) { xhr.overrideMimeType('application/json; charset=UTF-8'); } }; return $.ajax({ type:"POST", // method = "POST" url:"test.html", // POST送信先のURL, data:JSON.stringify(data), // JSONデータ本体 contentType: 'application/json; charset=UTF-8', // リクエストの Content-Type dataType: "json", // レスポンスをJSONとしてパースする }) .done(function(result) { console.log(result); }).fail(function() { // エラー alert("通信エラー。リトライしてください。"); }).always(function(result) { // 完了処理 }); }); });

Maven

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.3</version> </dependency>

Config

<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/plain;charset=UTF-8,application/json;charset=UTF-8,application/xml;charset=UTF-8,text/plain;charset=UTF-8" /> <property name="writeAcceptCharset" value="false" /> </bean> </mvc:message-converters> </mvc:annotation-driven>

###試したこと
Produces="application/json; charset=UTF-8"
を@RequestMappingに追加すればよいという情報は見つけられたのですが、それをするとJSから呼び出した際にMappingがうまくいかず、メソッドが呼び出せません。
そのためheadersに"Content-type=application/json;charset=UTF-8"を指定しています。(これで代用がきくかと考えていたのですがきかないのでしょうか・・・)
headersやconsumesについては手当たり次第つけてみたのですが効果は現れませんでした…

また、Web.xmlにはEncoding Filterを、Spring Securityより上に定義済みです。

###補足
Java8 Spring4.2 Tomcat8

###暫定対応
JsonをJava上で一度Unicodeにescapeし、JSで受け取ってからunescapeすることで何とか通信しています・・・が・・・遅い・・・

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

@RequestMappingproduces="application/json;charset=UTF-8"に設定を追加れば文字化けしなくなるかと。

java

1@RequestMapping(value = "/test", method = RequestMethod.POST, produces="application/json;charset=UTF-8) 2@ResponseBody 3public String child_kensaku(@RequestBody String inputdata , HttpServletRequest request, HttpServletResponse response, HttpSession session, SessionStatus sessionstatus 4 ) throws IllegalArgumentException, IllegalAccessException, ParseException, JsonProcessingException{ 5 ObjectMapper mapper = new ObjectMapper(); 6 String ret = mapper.writeValueAsString(jsonstr); //jsonstrに日本語のデータがある 7 8 return ret; 9}

検証してみましが、Mappingがうまくいかず、メソッドが呼び出せません。 という状況が再現できませんでした。
再現に使用したソースは以下の通りとなります

JDK8 u77
Eclipse 4.5 mars2
Tomcat 8.0.33 with log4j1.2
spring-webmvc 4.2.5.RELEASE

TestController.java

java

1package sandbox.slp.mvc; 2 3import java.text.ParseException; 4import java.util.Arrays; 5import java.util.List; 6import java.util.Map; 7 8import javax.servlet.http.HttpServletRequest; 9import javax.servlet.http.HttpServletResponse; 10import javax.servlet.http.HttpSession; 11 12import org.slf4j.Logger; 13import org.slf4j.LoggerFactory; 14import org.springframework.stereotype.Controller; 15import org.springframework.web.bind.annotation.RequestBody; 16import org.springframework.web.bind.annotation.RequestMapping; 17import org.springframework.web.bind.annotation.RequestMethod; 18import org.springframework.web.bind.annotation.ResponseBody; 19import org.springframework.web.bind.support.SessionStatus; 20 21import com.fasterxml.jackson.core.JsonProcessingException; 22 23@Controller 24public class TestController { 25 26 private static final Logger log = LoggerFactory.getLogger(TestController.class); 27 28 @RequestMapping(value = "/test/1", produces = "application/json;charset=UTF-8") 29 @ResponseBody 30 public String child_kensaku(@RequestBody String inputdata, HttpServletRequest request, HttpServletResponse response, 31 HttpSession session, SessionStatus sessionstatus) 32 throws IllegalArgumentException, IllegalAccessException, ParseException, JsonProcessingException { 33 log.info(inputdata); 34 return "{ \"name\":\"日本語\"}"; 35 } 36}

index.html

html

1<!DOCTYPE html> 2<html> 3<head> 4<title>jQuery ajax POST sample</title> 5<script 6 src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 7<script> 8 $(function() { 9 $("#ajax_btn").click(function ajaxpost() { 10 var formData = { 11 name : "日本語文字列入お名前", 12 age : "31" 13 }; 14 15 $.ajax({ 16 url : "test/1/", 17 type : "POST", 18 data : JSON.stringify(formData), 19 contentType : 'application/json;charset=UTF-8', // リクエストの Content-Type 20 dataType : "json", // レスポンスをJSONとしてパースする 21 success : function(data, textStatus, jqXHR) { 22 //data - response from server 23 console.log(data); 24 }, 25 error : function(jqXHR, textStatus, errorThrown) { 26 27 } 28 }); 29 }); 30 }); 31</script> 32</head> 33 34<body> 35 <input id="ajax_btn" type="button" value="1押す" /> 36</body> 37</html>

servlet-context.xml

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<beans:beans 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:beans="http://www.springframework.org/schema/beans" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 10 <mvc:annotation-driven /> 11 <mvc:resources 12 mapping="/resources/**" 13 location="/resources/" /> 14 <mvc:resources 15 mapping="/*" 16 location="/" /> 17</beans:beans>

pom.xml

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<project 3 xmlns="http://maven.apache.org/POM/4.0.0" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 6 <modelVersion>4.0.0</modelVersion> 7 <groupId>sandbox</groupId> 8 <artifactId>sandbox.slp</artifactId> 9 <name>sandbox.slp</name> 10 <packaging>war</packaging> 11 <version>1.0.0-BUILD-SNAPSHOT</version> 12 <properties> 13 <org.springframework-version>4.2.5.RELEASE</org.springframework-version> 14 </properties> 15 <dependencies> 16 <!-- Spring --> 17 <dependency> 18 <groupId>org.springframework</groupId> 19 <artifactId>spring-webmvc</artifactId> 20 <version>${org.springframework-version}</version> 21 </dependency> 22 <dependency> 23 <groupId>ch.qos.logback</groupId> 24 <artifactId>logback-classic</artifactId> 25 <version>1.1.7</version> 26 </dependency> 27 <dependency> 28 <groupId>javax.servlet</groupId> 29 <artifactId>javax.servlet-api</artifactId> 30 <version>3.1.0</version> 31 <scope>provided</scope> 32 </dependency> 33 <dependency> 34 <groupId>javax.servlet.jsp</groupId> 35 <artifactId>javax.servlet.jsp-api</artifactId> 36 <version>2.3.1</version> 37 <scope>provided</scope> 38 </dependency> 39 <dependency> 40 <groupId>javax.servlet</groupId> 41 <artifactId>jstl</artifactId> 42 <version>1.2</version> 43 </dependency> 44 <dependency> 45 <groupId>com.fasterxml.jackson.core</groupId> 46 <artifactId>jackson-databind</artifactId> 47 <version>2.7.3</version> 48 </dependency> 49 <dependency> 50 <groupId>com.fasterxml.jackson.core</groupId> 51 <artifactId>jackson-core</artifactId> 52 <version>2.7.3</version> 53 </dependency> 54 <dependency> 55 <groupId>com.fasterxml.jackson.core</groupId> 56 <artifactId>jackson-annotations</artifactId> 57 <version>2.7.3</version> 58 </dependency> 59 </dependencies> 60</project>

サーバ側でのログ出力

17:52:49.271 [http-nio-8080-exec-25] INFO sandbox.slp.mvc.TestController - {"name":"日本語文字列入お名前","age":"31"}

クライアント側でのログ出力

Object {name: "日本語"}

また、以下の依存関係を追加し、オブジェクトのままjsonstrを返却すれば@RequestMapping(value = "/test")のみで文字化けせずにJSONデータとして返却されるようになります。

maven

1<dependency> 2 <groupId>com.fasterxml.jackson.core</groupId> 3 <artifactId>jackson-databind</artifactId> 4 <version>2.7.3</version> 5</dependency>

投稿2016/04/15 18:08

編集2016/04/16 09:04
umed0025

総合スコア851

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zaganchan

2016/04/18 01:57 編集

検証までして頂いてありがとうございます。 Mappingのエラーは再現しないのですね・・・ 環境の伝え漏れがあるのかもしれません。申し訳ないです。 umed0025さんが検証されたことと同様のことを私の環境で実行しますと、 サーバ側でのログ出力 →日本語で正常に表記される。 クライアント側でのログ出力 →日本語は?に置換される。 jackson-databindを導入してjsonstrをそのまま返す →Jsonデータは作成されているが、日本語部分が全て?に置換されている。 という状況です。 関係あるかは分からないのですが、ThymeleafでHTMLを呼び出しています。それに当たってJSPの優先度を下げています。 servlet-context.xml ``` <!-- Thymeleaf --> <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" /> --> <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5" /> <property name="characterEncoding" value="UTF-8" /> </bean> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="templateEngine"> <bean class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> </property> <property name="order" value="2" /> <property name="characterEncoding" value="UTF-8" /> </bean> <!-- JSP --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> <property name="order" value="3" /> </bean> ``` jsonstrをそのまま返しても?に置換されるということは文字コードの問題ではなくて何か別の問題(サーブレットの設定等・・?)なのでしょうか… また、producesについてはリクエストヘッダのAccept部分、headerについてはリクエストヘッダを全て見るということで認識しているのですが、headerで指定していればProduceで指定するのと同等の効果が得られるということではないのでしょうか? 要領を得ない質問で申し訳ございませんが、どうかまたお返事いただければ幸いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問