无论我尝试什么,对调度程序servlet的请求都会返回HTTP 415错误。 Content-Type在请求中设置为application / json。
消息转换器似乎没有将请求映射到对象。
我在POM中拥有所有Jackson依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.7</version>
</dependency>
控制器类:
@RequestMapping(value = "/login", method = RequestMethod.POST, headers = {"Accept=application/json"}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DriverLoginResponseDTO login(@RequestBody MyDTO dto)
{
System.out.println("login method hit!");
LoginResponseDTO resp = null;
try {
resp = loginService.processLogin(dto);
}
catch (Exception cde) {
resp = new LoginResponseDTO();
resp.setMessage("ERROR");
}
// Return response object
return resp;
} // login
我尝试在@RequestMapping中添加accept,consume,production无效。
我可以使用HttpServletRequest作为方法参数获取正确的JSON响应:
@RequestMapping(value = "/login3", method = RequestMethod.POST)
@ResponseBody
public DriverLoginResponseDTO login3(HttpServletRequest request)
{
String line;
StringBuilder sb = new StringBuilder();
InputStream is;
try {
is = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("login3 method hit!");
System.out.println("JSON string received: " + sb.toString());
LoginResponseDTO resp = null;
try {
resp = loginService.processLogin(new MyDTO());
}
catch (Exception cde) {
resp = new LoginResponseDTO();
resp.setMessage("ERROR");
}
// Return response object
return resp;
} // login
这个在写入System.out时显示正确的JSON字符串。在上面的第一个“登录”方法中,有些东西没有将JSON请求映射到RequestBody对象,但是我看不到缺少的东西。
我不知道这是否相关,但Websphere也会抛出以下错误:
SRVE8094W:警告:无法设置标题。响应已经提交。
答案 0 :(得分:0)
尝试在consumes = "application/json"
headers = {"Accept=application/json"}
而不是@RequestMapping