当前位置: 首页 > 知识库问答 >
问题:

Jackson转换器和Javax验证(注释)不能一起工作

仲孙子辰
2023-03-14

如果我使用以下配置,那么jackson转换器工作(mvc声明是最后)

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />

如果我在dispatcher.xml中使用此配置,那么验证工作,但转换不工作。(mvc声明优先)

<context:component-scan base-package="com.base" />

<mvc:annotation-driven />

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>

非常感谢您的任何帮助。Spring版本4.0.6

共有1个答案

关胜
2023-03-14

我选择了验证工作的部分,并将其添加到代码库中。

@RequestMapping(value = "url", method = RequestMethod.GET)
protected void getLocationAsJson(@PathVariable("id")   Integer id,
 @RequestParam("cid") Integer cid, HttpServletResponse response) {
    MappingJacksonHttpMessageConverter jsonConverter = 
            new MappingJacksonHttpMessageConverter();
    Location loc= new Location(id);
    MediaType jsonMimeType = MediaType.APPLICATION_JSON;
    if (jsonConverter.canWrite(requestedLocation.getClass(), jsonMimeType)) {
    try {
        jsonConverter.write(requestedLocation, jsonMimeType,
                               new ServletServerHttpResponse(response));
        } catch (IOException m_Ioe) {
            // TODO: announce this exception somehow
        } catch (HttpMessageNotWritableException p_Nwe) {
            // TODO: announce this exception somehow
        }
    }
}

现在,验证和JSON返回一样有效。该方法不返回任何内容。

 类似资料:
  • 我有一个使用javax注释的jax-rs项目(

  • 我正在试验把杰克逊和龙目岛结合起来。这些是我的课: 这些是我添加到类spth中的JAR: > 龙目岛:https://projectlombok.org/downloads/lombok.jar(1.16.10version) 此外,Netbeans项目被配置为“保存时不编译”、“生成调试信息”、“报告不推荐使用的API”、“跟踪java依赖项”、“Activacte注释处理”和“编辑器中的Act

  • 我想验证属性firstName和lastName。但我不想每次都重复注释。 如何创建自定义注释,使代码如下所示 验证也是一样的

  • 我的springboot版本是2.3.7。我知道spring boot starter验证不是spring boot starter web的可传递依赖项。但即使单独添加了它,我的注释也不起作用。 //下面的依赖我已经添加build.gradle编译'org.springframework.boot: spring-boot-starter-validation' //我希望在请求时出错的示例类

  • 执行控制器方法时,我收到以下日志: ObjECT:[字段名称上的对象目录中的字段错误:拒绝值[safasf];代码[Pattern.catalog.name, Pattern.name, Pattern.java.lang.String, Pattern];参数[org.springframework.context.support.DefaultMessageSourceResolable:代码

  • 更新后反序列化失败。 我将我的微服务从更新到,并将从更新为,从更新为。 JSON字符串- 班级 - 方法调用- 用于反序列化的方法 - 错误 -