【问题标题】:Spring framework json character errorSpring框架json字符错误
【发布时间】:2016-05-30 18:29:38
【问题描述】:

我正在为 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);
}

【问题讨论】:

  • 听起来像是字符编码问题。您的应用程序期望什么字符编码?输入的编码是否与应用程序期望的不同?

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


【解决方案1】:

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

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

到您的请求映射

【讨论】:

    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多