質問するログイン新規登録

回答編集履歴

1

再現について追記

2016/04/16 09:04

投稿

umed0025
umed0025

スコア851

answer CHANGED
@@ -12,6 +12,191 @@
12
12
  }
13
13
  ```
14
14
 
15
+ 検証してみましが、`Mappingがうまくいかず、メソッドが呼び出せません。 `という状況が再現できませんでした。
16
+ 再現に使用したソースは以下の通りとなります
17
+
18
+ JDK8 u77
19
+ Eclipse 4.5 mars2
20
+ Tomcat 8.0.33 with log4j1.2
21
+ spring-webmvc 4.2.5.RELEASE
22
+
23
+ TestController.java
24
+
25
+ ```java
26
+ package sandbox.slp.mvc;
27
+
28
+ import java.text.ParseException;
29
+ import java.util.Arrays;
30
+ import java.util.List;
31
+ import java.util.Map;
32
+
33
+ import javax.servlet.http.HttpServletRequest;
34
+ import javax.servlet.http.HttpServletResponse;
35
+ import javax.servlet.http.HttpSession;
36
+
37
+ import org.slf4j.Logger;
38
+ import org.slf4j.LoggerFactory;
39
+ import org.springframework.stereotype.Controller;
40
+ import org.springframework.web.bind.annotation.RequestBody;
41
+ import org.springframework.web.bind.annotation.RequestMapping;
42
+ import org.springframework.web.bind.annotation.RequestMethod;
43
+ import org.springframework.web.bind.annotation.ResponseBody;
44
+ import org.springframework.web.bind.support.SessionStatus;
45
+
46
+ import com.fasterxml.jackson.core.JsonProcessingException;
47
+
48
+ @Controller
49
+ public class TestController {
50
+
51
+ private static final Logger log = LoggerFactory.getLogger(TestController.class);
52
+
53
+ @RequestMapping(value = "/test/1", produces = "application/json;charset=UTF-8")
54
+ @ResponseBody
55
+ public String child_kensaku(@RequestBody String inputdata, HttpServletRequest request, HttpServletResponse response,
56
+ HttpSession session, SessionStatus sessionstatus)
57
+ throws IllegalArgumentException, IllegalAccessException, ParseException, JsonProcessingException {
58
+ log.info(inputdata);
59
+ return "{ \"name\":\"日本語\"}";
60
+ }
61
+ }
62
+ ```
63
+ index.html
64
+
65
+ ```html
66
+ <!DOCTYPE html>
67
+ <html>
68
+ <head>
69
+ <title>jQuery ajax POST sample</title>
70
+ <script
71
+ src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
72
+ <script>
73
+ $(function() {
74
+ $("#ajax_btn").click(function ajaxpost() {
75
+ var formData = {
76
+ name : "日本語文字列入お名前",
77
+ age : "31"
78
+ };
79
+
80
+ $.ajax({
81
+ url : "test/1/",
82
+ type : "POST",
83
+ data : JSON.stringify(formData),
84
+ contentType : 'application/json;charset=UTF-8', // リクエストの Content-Type
85
+ dataType : "json", // レスポンスをJSONとしてパースする
86
+ success : function(data, textStatus, jqXHR) {
87
+ //data - response from server
88
+ console.log(data);
89
+ },
90
+ error : function(jqXHR, textStatus, errorThrown) {
91
+
92
+ }
93
+ });
94
+ });
95
+ });
96
+ </script>
97
+ </head>
98
+
99
+ <body>
100
+ <input id="ajax_btn" type="button" value="1押す" />
101
+ </body>
102
+ </html>
103
+ ```
104
+ servlet-context.xml
105
+
106
+ ```xml
107
+ <?xml version="1.0" encoding="UTF-8"?>
108
+ <beans:beans
109
+ xmlns:mvc="http://www.springframework.org/schema/mvc"
110
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
111
+ xmlns:beans="http://www.springframework.org/schema/beans"
112
+ xmlns:context="http://www.springframework.org/schema/context"
113
+ xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
114
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
115
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
116
+ <mvc:annotation-driven />
117
+ <mvc:resources
118
+ mapping="/resources/**"
119
+ location="/resources/" />
120
+ <mvc:resources
121
+ mapping="/*"
122
+ location="/" />
123
+ </beans:beans>
124
+ ```
125
+ pom.xml
126
+
127
+ ```xml
128
+ <?xml version="1.0" encoding="UTF-8"?>
129
+ <project
130
+ xmlns="http://maven.apache.org/POM/4.0.0"
131
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
132
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
133
+ <modelVersion>4.0.0</modelVersion>
134
+ <groupId>sandbox</groupId>
135
+ <artifactId>sandbox.slp</artifactId>
136
+ <name>sandbox.slp</name>
137
+ <packaging>war</packaging>
138
+ <version>1.0.0-BUILD-SNAPSHOT</version>
139
+ <properties>
140
+ <org.springframework-version>4.2.5.RELEASE</org.springframework-version>
141
+ </properties>
142
+ <dependencies>
143
+ <!-- Spring -->
144
+ <dependency>
145
+ <groupId>org.springframework</groupId>
146
+ <artifactId>spring-webmvc</artifactId>
147
+ <version>${org.springframework-version}</version>
148
+ </dependency>
149
+ <dependency>
150
+ <groupId>ch.qos.logback</groupId>
151
+ <artifactId>logback-classic</artifactId>
152
+ <version>1.1.7</version>
153
+ </dependency>
154
+ <dependency>
155
+ <groupId>javax.servlet</groupId>
156
+ <artifactId>javax.servlet-api</artifactId>
157
+ <version>3.1.0</version>
158
+ <scope>provided</scope>
159
+ </dependency>
160
+ <dependency>
161
+ <groupId>javax.servlet.jsp</groupId>
162
+ <artifactId>javax.servlet.jsp-api</artifactId>
163
+ <version>2.3.1</version>
164
+ <scope>provided</scope>
165
+ </dependency>
166
+ <dependency>
167
+ <groupId>javax.servlet</groupId>
168
+ <artifactId>jstl</artifactId>
169
+ <version>1.2</version>
170
+ </dependency>
171
+ <dependency>
172
+ <groupId>com.fasterxml.jackson.core</groupId>
173
+ <artifactId>jackson-databind</artifactId>
174
+ <version>2.7.3</version>
175
+ </dependency>
176
+ <dependency>
177
+ <groupId>com.fasterxml.jackson.core</groupId>
178
+ <artifactId>jackson-core</artifactId>
179
+ <version>2.7.3</version>
180
+ </dependency>
181
+ <dependency>
182
+ <groupId>com.fasterxml.jackson.core</groupId>
183
+ <artifactId>jackson-annotations</artifactId>
184
+ <version>2.7.3</version>
185
+ </dependency>
186
+ </dependencies>
187
+ </project>
188
+ ```
189
+ サーバ側でのログ出力
190
+
191
+ ```
192
+ 17:52:49.271 [http-nio-8080-exec-25] INFO sandbox.slp.mvc.TestController - {"name":"日本語文字列入お名前","age":"31"}
193
+ ```
194
+
195
+ クライアント側でのログ出力
196
+
197
+ ```
198
+ Object {name: "日本語"}
199
+ ```
15
200
  また、以下の依存関係を追加し、オブジェクトのまま`jsonstr`を返却すれば`@RequestMapping(value = "/test")`のみで文字化けせずにJSONデータとして返却されるようになります。
16
201
 
17
202
  ```maven