Skip to content

Commit a7ea7e9

Browse files
committed
cleanup work on lesson 5
1 parent 825bb72 commit a7ea7e9

52 files changed

Lines changed: 591 additions & 1079 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎m5-lesson1-start/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import javax.persistence.EntityNotFoundException;
66

77
import org.apache.commons.lang3.exception.ExceptionUtils;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
108
import org.springframework.dao.DataAccessException;
119
import org.springframework.dao.DataIntegrityViolationException;
1210
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -30,30 +28,23 @@
3028
import com.baeldung.common.web.exception.ValidationErrorDTO;
3129

3230
@ControllerAdvice
33-
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
34-
private Logger log = LoggerFactory.getLogger(getClass());
35-
36-
public RestResponseEntityExceptionHandler() {
37-
super();
38-
}
39-
40-
// API
31+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4132

4233
// 400
4334

4435
@Override
4536
protected final ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
46-
log.info("Bad Request: {}", ex.getMessage());
47-
log.debug("Bad Request: ", ex);
37+
logger.info("Bad Request: " + ex.getMessage());
38+
logger.debug("Bad Request: ", ex);
4839

4940
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
5041
return handleExceptionInternal(ex, apiError, headers, HttpStatus.BAD_REQUEST, request);
5142
}
5243

5344
@Override
5445
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
55-
log.info("Bad Request: {}", ex.getMessage());
56-
log.debug("Bad Request: ", ex);
46+
logger.info("Bad Request: " + ex.getMessage());
47+
logger.debug("Bad Request: ", ex);
5748

5849
final BindingResult result = ex.getBindingResult();
5950
final List<FieldError> fieldErrors = result.getFieldErrors();
@@ -63,9 +54,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6354
}
6455

6556
@ExceptionHandler(value = { DataIntegrityViolationException.class })
66-
public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
67-
log.info("Bad Request: {}", ex.getLocalizedMessage());
68-
log.debug("Bad Request: ", ex);
57+
protected final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
58+
logger.info("Bad Request: " + ex.getLocalizedMessage());
59+
logger.debug("Bad Request: ", ex);
6960

7061
if (ExceptionUtils.getRootCauseMessage(ex).contains("uplicate")) {
7162
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
@@ -79,7 +70,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7970
// 403
8071

8172
@ExceptionHandler({ AccessDeniedException.class })
82-
public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, final WebRequest request) {
73+
protected ResponseEntity<Object> handleEverything(final AccessDeniedException ex, final WebRequest request) {
8374
logger.error("403 Status Code", ex);
8475

8576
final ApiError apiError = message(HttpStatus.FORBIDDEN, ex);
@@ -91,7 +82,7 @@ public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, f
9182

9283
@ExceptionHandler({ EntityNotFoundException.class, MyEntityNotFoundException.class, MyResourceNotFoundException.class })
9384
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
94-
log.warn("Not Found: {}", ex.getMessage());
85+
logger.warn("Not Found: " + ex.getMessage());
9586

9687
final ApiError apiError = message(HttpStatus.NOT_FOUND, ex);
9788
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
@@ -101,7 +92,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
10192

10293
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class, MyConflictException.class })
10394
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
104-
log.warn("Conflict: {}", ex.getMessage());
95+
logger.warn("Conflict: " + ex.getMessage());
10596

10697
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
10798
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.CONFLICT, request);
@@ -110,7 +101,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
110101
// 500
111102

112103
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
113-
public ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
104+
protected ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
114105
logger.error("500 Status Code", ex);
115106

116107
final ApiError apiError = message(HttpStatus.INTERNAL_SERVER_ERROR, ex);

‎m5-lesson1-start/common/src/main/java/com/baeldung/common/web/exception/ApiError.java‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public class ApiError {
77
private String developerMessage;
88

99
public ApiError(final int status, final String message, final String developerMessage) {
10-
super();
11-
1210
this.status = status;
1311
this.message = message;
1412
this.developerMessage = developerMessage;
@@ -42,45 +40,6 @@ public void setDeveloperMessage(final String developerMessage) {
4240

4341
//
4442

45-
@Override
46-
public final int hashCode() {
47-
final int prime = 31;
48-
int result = 1;
49-
result = prime * result + ((developerMessage == null) ? 0 : developerMessage.hashCode());
50-
result = prime * result + ((message == null) ? 0 : message.hashCode());
51-
result = prime * result + status;
52-
return result;
53-
}
54-
55-
@Override
56-
public final boolean equals(final Object obj) {
57-
if (this == obj) {
58-
return true;
59-
}
60-
if (obj == null) {
61-
return false;
62-
}
63-
if (getClass() != obj.getClass()) {
64-
return false;
65-
}
66-
final ApiError other = (ApiError) obj;
67-
if (developerMessage == null) {
68-
if (other.developerMessage != null) {
69-
return false;
70-
}
71-
} else if (!developerMessage.equals(other.developerMessage)) {
72-
return false;
73-
}
74-
if (message == null) {
75-
if (other.message != null) {
76-
return false;
77-
}
78-
} else if (!message.equals(other.message)) {
79-
return false;
80-
}
81-
return status == other.status;
82-
}
83-
8443
@Override
8544
public final String toString() {
8645
final StringBuilder builder = new StringBuilder();

‎m5-lesson1-start/pom.xml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>org.springframework.boot</groupId>
1414
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>1.4.0.RELEASE</version>
15+
<version>1.4.3.RELEASE</version>
1616
<relativePath /> <!-- lookup parent from repository -->
1717
</parent>
1818

@@ -114,8 +114,7 @@
114114
<java.version>1.8</java.version>
115115

116116
<!-- Spring related -->
117-
<spring-hateoas.version>0.21.0.RELEASE</spring-hateoas.version>
118-
<spring-boot.version>1.4.0.RELEASE</spring-boot.version>
117+
<spring-hateoas.version>0.21.0.RELEASE</spring-hateoas.version>
119118

120119
<commons-collections.version>3.2.2</commons-collections.version>
121120
<hibernate.version>5.2.1.Final</hibernate.version>
@@ -128,7 +127,7 @@
128127
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
129128

130129
<!-- marshalling -->
131-
<jackson.version>2.8.5</jackson.version>
130+
<jackson.version>2.8.6</jackson.version>
132131
<jaxb-api.version>2.2.12</jaxb-api.version>
133132

134133
<!-- commons and utils -->

‎m5-lesson1-start/um-webapp/src/main/java/com/baeldung/um/persistence/setup/MyApplicationContextInitializer.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void initialize(final ConfigurableApplicationContext applicationContext)
3434
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env-" + envTarget + ".properties"));
3535

3636
final String activeProfiles = environment.getProperty("spring.profiles.active");
37-
environment.setActiveProfiles(activeProfiles.split(","));
37+
if (activeProfiles != null) {
38+
environment.setActiveProfiles(activeProfiles.split(","));
39+
}
3840
} catch (final IOException ioEx) {
3941
if (envTarget != null) {
4042
logger.warn("Didn't find env-" + envTarget + ".properties in classpath so not loading it in the AppContextInitialized", ioEx);

‎m5-lesson1-start/um-webapp/src/main/java/com/baeldung/um/service/StartupLoggingComponent.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public void afterPropertiesSet() {
3535
try {
3636
logEnvTarget(env);
3737
logPersistenceTarget(env);
38-
39-
logActiveSpringProfile(env);
38+
4039
logPersistenceData(env);
4140
} catch (final Exception ex) {
4241
logger.warn("There was a problem logging data on startup", ex);
@@ -55,12 +54,7 @@ private void logEnvTarget(final Environment environment) {
5554
private void logPersistenceTarget(final Environment environment) {
5655
final String envTarget = getValueOfProperty(environment, PERSISTENCE_TARGET_KEY, "h2", Lists.newArrayList("h2", "mysql", "cargo"));
5756
logger.info("{} = {}", PERSISTENCE_TARGET_KEY, envTarget);
58-
}
59-
60-
private void logActiveSpringProfile(final Environment environment) {
61-
final String activeSpringProfile = getValueOfProperty(environment, ACTIVE_SPRING_PROFILE_KEY, "none", null);
62-
logger.info("{} = {}", ACTIVE_SPRING_PROFILE_KEY, activeSpringProfile);
63-
}
57+
}
6458

6559
private void logPersistenceData(final Environment environment) {
6660
final String persistenceHost = getValueOfProperty(environment, PERSISTENCE_HOST_KEY, "not-found", null);

‎m5-lesson1/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import javax.persistence.EntityNotFoundException;
66

77
import org.apache.commons.lang3.exception.ExceptionUtils;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
108
import org.springframework.dao.DataAccessException;
119
import org.springframework.dao.DataIntegrityViolationException;
1210
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -31,29 +29,22 @@
3129

3230
@ControllerAdvice
3331
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
34-
private Logger log = LoggerFactory.getLogger(getClass());
35-
36-
public RestResponseEntityExceptionHandler() {
37-
super();
38-
}
39-
40-
// API
4132

4233
// 400
4334

4435
@Override
4536
protected final ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
46-
log.info("Bad Request: {}", ex.getMessage());
47-
log.debug("Bad Request: ", ex);
37+
logger.info("Bad Request: " + ex.getMessage());
38+
logger.debug("Bad Request: ", ex);
4839

4940
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
5041
return handleExceptionInternal(ex, apiError, headers, HttpStatus.BAD_REQUEST, request);
5142
}
5243

5344
@Override
5445
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
55-
log.info("Bad Request: {}", ex.getMessage());
56-
log.debug("Bad Request: ", ex);
46+
logger.info("Bad Request: " + ex.getMessage());
47+
logger.debug("Bad Request: ", ex);
5748

5849
final BindingResult result = ex.getBindingResult();
5950
final List<FieldError> fieldErrors = result.getFieldErrors();
@@ -63,9 +54,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6354
}
6455

6556
@ExceptionHandler(value = { DataIntegrityViolationException.class })
66-
public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
67-
log.info("Bad Request: {}", ex.getLocalizedMessage());
68-
log.debug("Bad Request: ", ex);
57+
protected final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
58+
logger.info("Bad Request: " + ex.getLocalizedMessage());
59+
logger.debug("Bad Request: ", ex);
6960

7061
if (ExceptionUtils.getRootCauseMessage(ex).contains("uplicate")) {
7162
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
@@ -79,7 +70,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7970
// 403
8071

8172
@ExceptionHandler({ AccessDeniedException.class })
82-
public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, final WebRequest request) {
73+
protected ResponseEntity<Object> handleEverything(final AccessDeniedException ex, final WebRequest request) {
8374
logger.error("403 Status Code", ex);
8475

8576
final ApiError apiError = message(HttpStatus.FORBIDDEN, ex);
@@ -91,7 +82,7 @@ public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, f
9182

9283
@ExceptionHandler({ EntityNotFoundException.class, MyEntityNotFoundException.class, MyResourceNotFoundException.class })
9384
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
94-
log.warn("Not Found: {}", ex.getMessage());
85+
logger.warn("Not Found: " + ex.getMessage());
9586

9687
final ApiError apiError = message(HttpStatus.NOT_FOUND, ex);
9788
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
@@ -101,7 +92,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
10192

10293
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class, MyConflictException.class })
10394
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
104-
log.warn("Conflict: {}", ex.getMessage());
95+
logger.warn("Conflict: " + ex.getMessage());
10596

10697
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
10798
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.CONFLICT, request);
@@ -110,7 +101,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
110101
// 500
111102

112103
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
113-
public ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
104+
protected ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
114105
logger.error("500 Status Code", ex);
115106

116107
final ApiError apiError = message(HttpStatus.INTERNAL_SERVER_ERROR, ex);

0 commit comments

Comments
 (0)