https://github.com/antonsjava/sb-utils
helper utils for spring boot projects
https://github.com/antonsjava/sb-utils
Last synced: about 2 months ago
JSON representation
helper utils for spring boot projects
- Host: GitHub
- URL: https://github.com/antonsjava/sb-utils
- Owner: antonsjava
- License: apache-2.0
- Created: 2023-10-29T16:43:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-01T14:59:09.000Z (9 months ago)
- Last Synced: 2025-02-01T21:17:25.530Z (4 months ago)
- Language: Java
- Size: 74.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sb-utils
helper utils for spring boot projects## usage info
Set of utilities. Mostly for logging soap and rest requests.
- branch sb2 (vsesions 2.x) for spring boot 2.x.x
- branch sb3 (vsesions 3.x) for spring boot 3.x.ximplementation uses runtime dependece for
- io.github.antonsjava:json for json messages formatting
- io.github.antonsjava:jaul for xml messages formattingif you use this functionality add those dependece to your pom.
## JsonExceptionHandler
Helper class for creating exception advice
- it logs exception
- it searches for exception with defined http code
- if such exception not exists root exception is used
- converts exception to jsonHttp code for exception is determined ba
- Exception class is annotated with ResponseStatus (@ResponseStatus(HttpStatus.BAD_GATEWAY))
- Exception class has method public static int httpCode()
- Exception class has http code determined in builder .statusResolver(JsonExceptionHandler.DefaultStatusResolver.instance().status(MyAppException.class, HttpStatus.CONFLICT))~~~java
@ControllerAdvice(basePackages = "sk.antons.project.api")
public class JsonExceptionAdvice {
private static Logger log = LoggerFactory.getLogger(JsonExceptionAdvice.class);
JsonExceptionHandler handler = JsonExceptionHandler.instance()
.logger(t -> log.info("request failed {} ", Stk.trace(t)))
// this is optional .statusResolver(JsonExceptionHandler.DefaultStatusResolver.instance().status(MyAppException.class, HttpStatus.CONFLICT))
// this is default .processor(JsonExceptionHandler.DefaultExceptionProcessor.instance())
);
{@code @}ExceptionHandler(Throwable.class)
public ResponseEntity{@code } throwable(final Throwable ex) {
return handler.process(ex);
}
}
~~~## RestTemplateClient
Simple wrapper over RestTemplate to call http requests
~~~java
private RestTemplateClient client;
private RestTemplateClient client() {
if(client == null) {
client = RestTemplateClient.Builder.instance()
.template(template) // RestTemplate instance
.headers(RestTemplateClient.Headers.builder() // header modifier
.add("Host", host)
.basicAuth(user, password)
.build()
)
.root("https://somethere.com/api") // root for urls
.client();
}
return client;
}
~~~and usage
~~~java
RestTemplateClient.Request request = client()
.get() //method
.path("/item/123"); //path
MyItem data = request.call(MyItem.class);
~~~~~~java
RestTemplateClient.Request request = client()
.post() //method
.content(myItem)
.path("/item"); //path
request.call(MyItem.class);
~~~## LoggingInterceptor
simple http client logging interceptor
~~~java
LoggingInterceptor.instance()
.requestHeaders(LoggingInterceptor.Headers.all()) // which headers to print
.requestBody(LoggingInterceptor.Body.json().forceOneLine().transform()) // how body is formatted
.responseHeaders(LoggingInterceptor.Headers.all()) // which headers to print
.responseBody(LoggingInterceptor.Body.json().forceOneLine().transform()) // how body is fomratted
.loggerEnabled( () -> true) // if interceptor is enabled
.logger(m -> System.out.println(m)) // how to log message
.addToTemplate(template); // add interceptor to template
~~~## CxfLogInterceptor
simple webcservice client log interceptor for cxf.
~~~java
CxfLogInterceptor.out()
// optional .format(is -> formatInputStreamToOneLineXml(is))
.loggerEnabled( () -> true) // if interceptor is enabled
.logger(m -> System.out.println(m)) // how to log message
~~~## SBWSLoggingInterceptor
simple web service client log interceptor for SB web services.
~~~java
SBWSLoggingInterceptor.instance()
// optional .format(is -> formatInputStreamToOneLineXml(is))
.loggerEnabled( () -> true) // if interceptor is enabled
.logger(m -> System.out.println(m)) // how to log message
~~~