spring 自定义 jackson BeanSerializerModifier后 null值不显示的问题处理

spring mvc 返回的对象假如说是

那么实际上 显示的返回结果是 :

实际上 xx 这个null值没有返回

但重写BeanSerializerModifier 中的这个方法,如果没有null序列化类的话设置上一个后结果

 输出结果:

原因:

在 BeanPropertyWriter 类中的serializeAsField 方法中,如果没有nullSerirlizer的话不做任何处理,propertiewriter 自定义之后是没有 nullSeriliser属性了导致null值不处理。但尝试用jackson的 Null注解在属性上也是不起作用的,待继续研究下

待研究

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在实现Spring Security自定义异常时,您需要做以下几个步骤: 1. 创建自定义异常类 您可以创建一个自定义异常类,例如`CustomAuthenticationException`,继承Spring Security的`AuthenticationException`类。在该类中添加构造函数,以便在抛出异常时能够提供有用的信息。 ```java public class CustomAuthenticationException extends AuthenticationException { public CustomAuthenticationException(String msg) { super(msg); } } ``` 2. 创建自定义异常处理类 创建一个`CustomAuthenticationEntryPoint`类,实现Spring Security的`AuthenticationEntryPoint`接口,并实现其中的`commence()`方法。在该方法中,您可以自定义响应的HTTP状态码、响应头和响应体等。例如,下面的代码将返回401状态码和自定义的响应信息。 ```java @Component public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { response.setStatus(HttpStatus.UNAUTHORIZED.value()); response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.getWriter().write(new ObjectMapper().writeValueAsString(new ErrorResponse(HttpStatus.UNAUTHORIZED, authException.getMessage()))); } } ``` 3. 在Security配置中使用自定义异常处理类 在您的Security配置中,使用`CustomAuthenticationEntryPoint`类来处理未经身份验证的请求。例如: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationEntryPoint customAuthenticationEntryPoint; @Override protected void configure(HttpSecurity http) throws Exception { http.exceptionHandling() .authenticationEntryPoint(customAuthenticationEntryPoint); // other security configurations } // other methods } ``` 这样,当未经身份验证的请求发生时,将会触发`CustomAuthenticationEntryPoint`类中的`commence()`方法,返回自定义的响应信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值