Skip to content

Commit 1d768aa

Browse files
committed
异常出力,及统一返回
1 parent e7d515c commit 1d768aa

File tree

5 files changed

+108
-7
lines changed

5 files changed

+108
-7
lines changed

samples/blog-user/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ dependencies {
7474
// 添加 Thymeleaf Spring Security 依赖,与 Thymeleaf 版本一致都是 3.x
7575
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE')
7676

77+
// 添加 Apache Commons Lang 依赖
78+
compile('org.apache.commons:commons-lang3:3.5')
79+
7780
// 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依
7881
testCompile('org.springframework.boot:spring-boot-starter-test')
7982

samples/blog-user/src/main/java/com/waylau/spring/boot/blog/controller/UserController.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import javax.validation.ConstraintViolationException;
7+
68
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.data.domain.Page;
810
import org.springframework.data.domain.PageRequest;
@@ -24,6 +26,8 @@
2426
import com.waylau.spring.boot.blog.domain.User;
2527
import com.waylau.spring.boot.blog.service.AuthorityService;
2628
import com.waylau.spring.boot.blog.service.UserService;
29+
import com.waylau.spring.boot.blog.util.ConstraintViolationExceptionHandler;
30+
import com.waylau.spring.boot.blog.vo.Response;
2731

2832

2933
/**
@@ -82,12 +86,17 @@ public ModelAndView createForm(Model model) {
8286
* @return
8387
*/
8488
@PostMapping
85-
public ResponseEntity<User> create(User user, Long authorityId) {
89+
public ResponseEntity<Response> create(User user, Long authorityId) {
8690
List<Authority> authorities = new ArrayList<>();
8791
authorities.add(authorityService.getAuthorityById(authorityId));
8892
user.setAuthorities(authorities);
89-
userService.saveUser(user);
90-
return ResponseEntity.ok().body( user);
93+
try {
94+
userService.saveUser(user);
95+
} catch (ConstraintViolationException e) {
96+
return ResponseEntity.ok().body(new Response(false, ConstraintViolationExceptionHandler.getMessage(e)));
97+
}
98+
99+
return ResponseEntity.ok().body(new Response(true, "处理成功", user));
91100
}
92101

93102
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
*
3+
*/
4+
package com.waylau.spring.boot.blog.util;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import javax.validation.ConstraintViolation;
10+
import javax.validation.ConstraintViolationException;
11+
12+
import org.apache.commons.lang3.StringUtils;
13+
14+
/**
15+
* @author Administrator
16+
*
17+
*/
18+
public class ConstraintViolationExceptionHandler {
19+
20+
/**
21+
* 获取批量异常信息
22+
* @param e
23+
* @return
24+
*/
25+
public static String getMessage(ConstraintViolationException e) {
26+
List<String> msgList = new ArrayList<>();
27+
for (ConstraintViolation<?> constraintViolation : e.getConstraintViolations()) {
28+
msgList.add(constraintViolation.getMessage());
29+
}
30+
String messages = StringUtils.join(msgList.toArray(), ";");
31+
return messages;
32+
}
33+
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.waylau.spring.boot.blog.vo;
2+
3+
/**
4+
* 响应 值对象.
5+
*
6+
* @since 1.0.0 2017年4月4日
7+
* @author <a href="https://waylau.com">Way Lau</a>
8+
*/
9+
public class Response {
10+
11+
12+
private boolean success;
13+
private String message;
14+
private Object body;
15+
16+
/** 响应处理是否成功 */
17+
public boolean isSuccess() {
18+
return success;
19+
}
20+
public void setSuccess(boolean success) {
21+
this.success = success;
22+
}
23+
24+
/** 响应处理的消息 */
25+
public String getMessage() {
26+
return message;
27+
}
28+
public void setMessage(String message) {
29+
this.message = message;
30+
}
31+
32+
/** 响应处理的返回内容 */
33+
public Object getBody() {
34+
return body;
35+
}
36+
public void setBody(Object body) {
37+
this.body = body;
38+
}
39+
40+
public Response(boolean success, String message) {
41+
this.success = success;
42+
this.message = message;
43+
}
44+
45+
public Response(boolean success, String message, Object body) {
46+
this.success = success;
47+
this.message = message;
48+
this.body = body;
49+
}
50+
51+
}

samples/blog-user/src/main/resources/static/js/users/main.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $(function() {
5050
success: function(data){
5151
$("#userFormContainer").html(data);
5252
},
53-
error : function() {
53+
error : function(data) {
5454
alert("error");
5555
}
5656
});
@@ -77,9 +77,13 @@ $(function() {
7777
type: 'POST',
7878
data:$('#userForm').serialize(),
7979
success: function(data){
80-
81-
// 从新刷新主界面
82-
getUersByName(0, _pageSize);
80+
if (data.success) {
81+
// 从新刷新主界面
82+
getUersByName(0, _pageSize);
83+
} else {
84+
alert(data.message);
85+
}
86+
8387
},
8488
error : function() {
8589
alert("error");

0 commit comments

Comments
 (0)