使用jackson过滤掉空值字段

This commit is contained in:
lsy
2018-01-09 15:08:26 +08:00
parent 63f0a371fd
commit 7bc79359a5
6 changed files with 45 additions and 20 deletions
@@ -5,6 +5,7 @@ import com.micro.fast.common.response.ServerResponse;
import com.micro.fast.upms.pojo.UpmsUser;
import com.micro.fast.upms.service.UpmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
@@ -0,0 +1,13 @@
package com.micro.fast.upms.controller.handler;
import com.micro.fast.common.exception.handler.BaseExceptionHandlerController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* upms异常处理基类
* @author lsy
*/
@RestControllerAdvice(basePackages = {"com.micro.fast.upms.controller"})
public class UpmsExceptionHandlerController extends BaseExceptionHandlerController{
}
@@ -11,12 +11,14 @@ public interface UpmsUserMapper {
int insert(UpmsUser record);
int insertSelective(UpmsUser record);
UpmsUser selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(UpmsUser record);
int updateByPrimaryKey(UpmsUser record);
/**
* 根据用户名查找用户
* @param username 用户名
@@ -37,10 +39,4 @@ public interface UpmsUserMapper {
* @return
*/
int countEmail(String email);
int updateByPrimaryKeySelective(UpmsUser record);
int updateByPrimaryKey(UpmsUser record);
}
@@ -1,23 +1,28 @@
package com.micro.fast.upms.pojo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.micro.fast.common.response.BaseConst;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UpmsUser implements Serializable {
private Integer id;
/**
* 用户名必须为6-16位字母或数字的组合
*/
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请按要求输入用户名")
@Pattern(regexp = "^[a-z,A-Z,0-9]{6,16}$",message = BaseConst.BASEMSG_PREFIX+"请按要求输入用户名")
private String username;
/**
* 密码必须为6-16位字母或数字的组合
*/
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请按要求输入密码")
@Pattern(regexp = "^[a-z,A-Z,0-9]{6,16}$",message = BaseConst.BASEMSG_PREFIX+"请按要求输入密码")
private String password;
@@ -29,6 +34,7 @@ public class UpmsUser implements Serializable {
private String phone;
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请按要求输入邮箱")
@Email(message = BaseConst.BASEMSG_PREFIX+"邮箱格式不正确")
private String email;
@@ -8,6 +8,7 @@ import com.micro.fast.upms.pojo.UpmsUser;
import com.micro.fast.upms.service.UpmsUserService;
import com.micro.fast.util.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.micro.fast.common.response.BaseConst.*;
@@ -17,6 +18,9 @@ import com.micro.fast.common.response.BaseConst.*;
@Service(value = "upmsUserServiceImpl")
public class UpmsUserServiceImpl implements UpmsUserService {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private UpmsUserMapper upmsUserMapper;
@@ -45,6 +49,8 @@ public class UpmsUserServiceImpl implements UpmsUserService {
,UserResponse.EMAIL_IS_HAVE.getMsg()));
}
upmsUser.setCtime(Long.valueOf(System.currentTimeMillis()));
//对密码进行加密处理
upmsUser.setPassword(passwordEncoder.encode(upmsUser.getPassword()));
int i = upmsUserMapper.insertSelective(upmsUser);
//是否插入成功
if (i<1){
@@ -25,18 +25,6 @@
from upms_user
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByUsername" resultType="com.micro.fast.upms.pojo.UpmsUser">
SELECT
<include refid="Base_Column_List"/>
FROM upms_user
WHERE username = #{username}
</select>
<select id="countUsername" resultType="java.lang.Integer">
SELECT COUNT(username) FROM upms_user WHERE username = #{username}
</select>
<select id="countEmail" resultType="java.lang.Integer">
SELECT COUNT(email) FROM upms_user WHERE email = #{email}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from upms_user
where id = #{id,jdbcType=INTEGER}
@@ -174,4 +162,19 @@
ctime = #{ctime,jdbcType=BIGINT}
where id = #{id,jdbcType=INTEGER}
</update>
<!--非自定生成-->
<select id="selectByUsername" resultType="com.micro.fast.upms.pojo.UpmsUser">
SELECT
<include refid="Base_Column_List"/>
FROM upms_user
WHERE username = #{username}
</select>
<select id="countUsername" resultType="java.lang.Integer">
SELECT COUNT(username) FROM upms_user WHERE username = #{username}
</select>
<select id="countEmail" resultType="java.lang.Integer">
SELECT COUNT(email) FROM upms_user WHERE email = #{email}
</select>
</mapper>