[英]Spring framework json character error
我正在为 SOA 休息服务使用 spring 框架。 由于字符有问题,我的服务运行良好。 我的服务不接受非英文字符。例如; IıĞğÜüŞşİiÖöÇç => I???Üü???iÖöÇç。 我用谷歌搜索了这个,但我找不到解决方案。 谢谢你的建议:)
我的 web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:com/yildizapp/resources/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>yappServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:com.yildizapp.resources.web/applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>yappServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
我的 applicationContext-web.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
我的控制器
@Autowired
private UserServiceDao userServiceDao;
@RequestMapping(value = "/register",method = RequestMethod.POST,produces = "application/json; charset=UTF-8 ")
@ResponseBody
public void saveUser(@RequestBody UserServiceRequest userServiceRequest){
UserService userService=new UserService();
userService.setName(userServiceRequest.getName());
userService.setSurname(userServiceRequest.getSurname());
userService.setPhoneNumber(userServiceRequest.getPhoneNumber());
userService.setUsername(userServiceRequest.getUsername());
userService.setPassword(userServiceRequest.getPassword());
userService.setEmail(userServiceRequest.getEmail());
userService.setDepartment(userServiceRequest.getDepartment());
userServiceDao.save(userService);
}
也许某些配置误导了杰克逊。 你可以尝试添加
consumes = "application/json; charset=UTF-8 "
到您的请求映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.