Spring框架json字符错误

时间:2016-05-30 18:29:38

标签: java json spring spring-mvc character-encoding

我正在使用Spring框架进行SOA休息服务。我的服务很好,我遇到了人物问题。我的服务不接受非英文字符。例如; IıĞğÜüŞşİiÖöÇç=>我??? ??? UUiÖöÇç。我用谷歌搜索了这个但我无法找到解决方案。感谢您的建议:)

我的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);
}

1 个答案:

答案 0 :(得分:0)

也许有些配置会误导杰克逊。您可以尝试添加

consumes = "application/json; charset=UTF-8 "

到您的请求映射