我有一个问题。 我正在使用Spring框架,我想返回Json数据
当我调用时,它会在 Postman 中返回 XML 类型。
但在控制台中打印了 Json 类型
这是 Controller
@Autowired
MobileUserService mobileUserService;
@RequestMapping(value = "/mobile/mobileuser.do")
@ResponseBody
public HashMap<String, Object> selectUser(
@RequestParam(value="userId", required = false) String userId,
@RequestParam(value="userPwd", required = false) String userPwd,
HttpServletRequest request) {
HashMap<String, Object> selectMap = mobileUserService.selectUser(userId, userPwd);
System.out.println("userid: " + userId + "userPW : " + userPwd);
if(selectMap == null) {
selectMap = new HashMap<String, Object>();
selectMap.put("code", -1);
selectMap.put("msg", "Check ID or Password");
}else {
selectMap.put("code", 0);
selectMap.put("msg", "Success");
}
System.out.println("sdfsdfsdf " + selectMap);
return selectMap;
}
我在 pom.xml 中添加了依赖项
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
服务器上下文.xml
<bean id=“jacksonMessageConverter” class=“org.springframework.http.converter.json.MappingJackson2HttpMessageConverter”/>
我怎么能这样?
最佳答案
将您的请求映射更改为
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
关于java - Spring - 为什么返回值在屏幕中显示 XML 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110106/