Last active
March 20, 2021 02:56
-
-
Save ekosutrisno/102496405c3ea2c6971645d929c9014f to your computer and use it in GitHub Desktop.
ConfigEnvironment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
/** | |
* @author Eko Sutrisno | |
* Saturday, 20/03/2021 9:31 | |
*/ | |
public class ExceptionHandlingResponse<T> { | |
private int statusCode; | |
private String status; | |
private Date timestamp; | |
private T error; | |
public ExceptionHandlingResponse(int statusCode, String status, Date timestamp, T error) { | |
this.statusCode = statusCode; | |
this.status = status; | |
this.timestamp = timestamp; | |
this.error = error; | |
} | |
public int getStatusCode() { | |
return statusCode; | |
} | |
public void setStatusCode(int statusCode) { | |
this.statusCode = statusCode; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public Date getTimestamp() { | |
return timestamp; | |
} | |
public void setTimestamp(Date timestamp) { | |
this.timestamp = timestamp; | |
} | |
public T getError() { | |
return error; | |
} | |
public void setError(T error) { | |
this.error = error; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.RestControllerAdvice; | |
import javax.validation.ConstraintViolationException; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static org.springframework.http.HttpStatus.*; | |
/** | |
* @author Eko Sutrisno | |
* Saturday, 20/03/2021 9:30 | |
*/ | |
@RestControllerAdvice | |
public class ExceptionHandling { | |
@ExceptionHandler(value = {ConstraintViolationException.class}) | |
public ExceptionHandlingResponse<Map<String, String>> validationHandler(ConstraintViolationException exception) { | |
Map<String, String> errors = new HashMap<>(); | |
exception.getConstraintViolations() | |
.forEach(error -> errors.put(error.getPropertyPath().toString(), error.getMessage())); | |
return new ExceptionHandlingResponse<>(BAD_REQUEST.value(), BAD_REQUEST.getReasonPhrase(), new Date(), errors); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.List; | |
import java.util.Optional; | |
public interface IOperations<T extends Serializable> { | |
Optional<T> findById(final Long id); | |
List<T> findAll(); | |
T create(final T entity); | |
T update(final T entity); | |
void delete(final T entity); | |
void deleteById(final long entityId); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger2</artifactId> | |
<version>2.9.2</version> | |
</dependency> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger-ui</artifactId> | |
<version>2.9.2</version> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableSwagger2 | |
public class SwaggerConfig { | |
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = | |
new HashSet<>(Arrays.asList("application/json", | |
"application/json")); | |
@Bean | |
public Docket swaggerConfiguration() { | |
return new Docket(DocumentationType.SWAGGER_2).select() | |
.apis(RequestHandlerSelectors.basePackage("com.erajaya.salesorderservice")) | |
.paths(PathSelectors.regex("/.*")) | |
.build().groupName("Sales Order") | |
.apiInfo(apiInfo()) | |
.useDefaultResponseMessages(false) | |
.produces(DEFAULT_PRODUCES_AND_CONSUMES) | |
.consumes(DEFAULT_PRODUCES_AND_CONSUMES); | |
} | |
private ApiInfo apiInfo() { | |
return new ApiInfo( | |
"Sales Order Service", | |
"Sales Order REST API", | |
"0.0.1", | |
"", | |
new Contact("Eko Sutrisno", | |
"", | |
"[email protected]"), | |
"Apache 2.0", | |
"http://www.apache.org/licenses/LICENSE-2.0.html", | |
Collections.emptyList()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.ConstraintViolationException; | |
import javax.validation.Validator; | |
import java.util.Set; | |
/** | |
* @author Eko Sutrisno | |
* Saturday, 20/03/2021 9:25 | |
*/ | |
@Component | |
public class ValidationConfigurationException { | |
private final Validator validator; | |
@Autowired | |
public ValidationConfigurationException(Validator validator) { | |
this.validator = validator; | |
} | |
public void validate(Object object) { | |
Set<ConstraintViolation<Object>> error = validator.validate(object); | |
if (error.size() != 0) | |
throw new ConstraintViolationException(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment