Using #Timed in service method - java

I'm working with Spring Boot, and I want to annotate some of my #Service methods with #Timed so I could view their statistics calling the metrics endpoint.
I've read from the Spring documentation that is can use dropwizard registry, but I can't find any example in which it uses dropwizar annotations.
How can I configure my Spring Boot app so each method annotated with #Timed automatically publishes its statistics using the build-in Spring actuator module?

Have you had a look at: http://www.ryantenney.com/metrics-spring/
Seems that you need to include that library if you want to use Spring with Metrics 3.0 / 3.1.

Related

Parse JsonSchema into io.swagger.v3.oas.models.media.Schema

I have spring boot app that provides openapi. I've used Swagger. I have a file that stores jsonSchema and I wanna to parse it into io.swagger.v3.oas.models.media.Schema to add it into Swagger. I can't find any existing solutions. Does any spring boot library supports this functionality?
Thanks.
If you are using Spring web MVC HATEOS endpoints, then you can directly use the https://springdoc.org/. Example: #RequestMapping, #GetMapping, etc.
Springdoc-openapi comes with ui and open api v3 docs generation.
IF not, and you prefer to use JaxRS2 to configure your RESTful endpoints, then use the <groupId>io.swagger.core.v3</groupId><artifactId>swagger-jaxrs2</artifactId>, and register your resources using JerseyConfig extends ResourceConfig and include io.swagger.v3.integration.sources for jaxrs2 scanning.

how handle Crosscutting concerns in jersy jaxrs

My problem is when we use Spring boot like framework, we can easily handle AOP. but how we handle AOP with jersy jaxrs project
Spring Boot is just a bootstrapping framework. For REST we can use Spring Boot to bootstrap Spring MVC or Jersey fully integrated with Spring. If you choose the latter route, then you can use the Spring AOP with Jersey. All you need to do is make your Jersey resources Spring #Components to able to intercept them. See an official example of Spring Boot and Jersey.
If you don't want to use Spring Boot to bootstrap your Jersey app, then you can still integrate Spring with Jersey. Remember the AOP is not tied to Spring Boot, it is tied to the Spring Framework, which are different things. You can see an example of Jersey with Spring (without Spring Boot) here.
If you don't want to involve Spring at all, then Jersey has a DI framework, HK2, which has it's own AOP. You can see a full example here

Is it possible to use #AutoConfigureRestDocs with restassured instead of mockmvc?

Spring Boot 1.4 introduces #AutoConfigureRestDocs (See http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs). The docs state:
It will automatically configure MockMvc to use Spring REST Docs and remove the need for Spring REST Docs' JUnit rule.
I would like to use the restassured support of Spring REST Docs instead of mockmvc. Can I do this with #AutoConfigureRestDocs ?
No, not at the moment. #AutoConfigureRestDocs builds on top of Spring Boot's testing support being able to auto-configure MockMvc. There's no support for auto-configuring REST Assured which the REST Docs auto-configuration could then build upon. If it's something that you'd like to see in Spring Boot, please open an enhancement request.

whats the jersey analog for spring (boot) data's #Repository

ive been tasked with a total refactor of legacy code. It's a simple webservice, just an http request, then business logic with possibly a few database calls and a few other microservice calls, then a json response. I am being pushed not to use spring boot because no one else around me has used it before, and I was told jersey does everything spring boot does. I've never used jersey so im trying to find out how to do things that spring boot makes simple (ie repository layer with spring-data, caching, spring-consul, spring-zuul, spring-actuator, spring-circuit-breaker) It looks like jersey does do an analog of spring-security, bean validation, and easy insertion of servlet filters, but not everything spring-boot does. Is there an easy way to wire in a JPA type repository in jersey? I cant find it in the docs at https://jersey.java.net/documentation/latest/index.html.
I think about it this way. There are different layers in your application. You have a service layer, and you have a "REST layer". You access the Spring repositories with the service layer. Then you have the REST layer. With Spring, you have Spring MVC which is its web layer implementation, that you can also use as REST services. There is also Jersey, which is completely independent of Spring, which is a another REST layer options.
That be said, when using Spring MVC as the REST layer, adding the service layer with Spring data is seamless. But Jersey also has integration with Spring, that allows us to use Spring at the service layer inside our Jersey REST services. You check out this post which has some links to example of how this can be done (no hacking, this is supported out of the box). Using this approach, you can just injector your Spring data repositories into your Jersey resource class
interface PetsRepository extends JpaRepository<Pets, Long> {}
#Path("/pets")
class PetsResource {
#Autowired
private PetsRepository repo;
}
Now lets talk about Spring Boot. Spring Boot is just a bootstrapping framework. What it does is allow you to easily bootstrap an application without all the boilerplate configuration you would need without it. When your using Spring Boot for your REST services, you're not actually using Spring Boot itself as the REST service engine. You are only using it to bootstrap Spring MVC and maybe your Spring Data. But Spring MVC is the actual REST service engine.
Now like I said before, Jersey has support for integrating Spring into into it (for the service layer). Because of this support, Spring Boot has also provided a bootstrap configuration to integrate this support seamlessly. So instead of using the manual configuration that you would see in one of the examples linked to above, Spring Boot handles this configuration for us. So we can use Jersey as the REST layer, and Spring beans as the service layer. Check out the links below
See also:
Spring Boot docs for Jersey support

How can I add one more metric to Spring Boot?

Spring Boot has preconfigured metrics. As I know it uses http://metrics.codahale.com/ library. How can I get MetricRegistry object to add my custom metrics?
Spring boot now supports Codahale Metrics out of the box.
See http://docs.spring.io/spring-boot/docs/1.0.0.RC5/reference/htmlsingle/#production-ready-code-hale-metrics
Spring Boot Does not use Codahale Metrics (yet). The plan is to support it as an option if it is on the classpath. If you prefer to do it that way then a MetricRegistry will be in the application context and you can simply #Autowire it and use it. You can see that work in progress on a branch in my fork.
The Boot interfaces for adding metrics are GaugeService and CounterService. You inject those and use them to record measurements. When Codahale gets support then this will be the recommended entry point for that as well, so you can get started using it now and add the Codahale stuff later if you want.
There's some integration magic accomplished by http://www.ryantenney.com/metrics-spring/ that wires codahale metrics into the actuator /health endpoint.
With this dependency included,
compile 'com.ryantenney.metrics:metrics-spring:3.0.0-RC2'
you can "enableMetrics" in your application configuration
#EnableMetrics
public class ApplicationConfiguration {
...
This allows you to time each request with the #timed annotation:
#Timed
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody
Foo foo() {
...
and to have your other interactions with MetricRegistry aggregate into the actuator /health endpoint.
I've put together an example application which implements this integration:
https://github.com/benschw/consul-cluster-puppet/tree/master/demo
and written a more in depth tutorial here:
http://txt.fliglio.com/2014/10/spring-boot-actuator/

Categories

Resources