springmvc jackon 为什么不返回json
发布网友
发布时间:2022-10-08 02:53
我来回答
共1个回答
热心网友
时间:2023-11-02 04:51
SpringMvc配置:
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!--json转换器-->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
Controller代码
@RequestMapping(value="update",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> update(SOUser entity,MultipartHttpServletRequest request){
Long userId = entity.getId();
Long uid = TokenManager.getUserId();
if(StringUtils.isBlank(userId,uid) || !uid.equals(userId)){
resultMap.put("status", 500);
resultMap.put("message", "非法修改!");
}else{
String name = "portrait/" + userId;
String portrait = ImageManager.saveImages(name, request);
if(StringUtils.isNotBlank(portrait)){
entity.setPortrait(portrait);
}
userService.updateByPrimaryKeySelective(entity);
resultMap.put("status", 200);
resultMap.put("message", "修改成功!");
entity = userService.selectByPrimaryKey(userId);
//更新
TokenManager.login(entity);
}
return resultMap;
}