Micronaut Commands

Published: 2019-03-12, Updated: 2020-05-12

Links

Ciclo de vida

Http requests

Parsear data que vem no parameter @QueryValue

@Get(value = "/fruits")
public void stuff(@QueryValue @Format("yyyy-MM-dd") LocalDate from){
...

Chamar método ao startar o app

@EventListener
public void eventListener(ApplicationStartupEvent startupEvent){
...
}

Injetar um bean exatamente no startup da app

@Context
public String someBean() {
	return "";
}

Para que serve o micronaut-spring

Ele permite que quem usa spring possa migrar facilmente para o micronaut, basicamente ele vai entender as anotações do spring e injeta-las como beans micronaut ao subir

Profiles and environment / active profiles

export MICRONAUT_ENVIRONMENTS=prod

Buildar imagem nativa

Trabalhando com requests e responses

Configurando TemplateParser

Configurar static resources

Testing

Criar mock para beans

@MicronautTest
class MathMockServiceTest {

  @Inject
  MathService mathService; 


  @ParameterizedTest
  @CsvSource({"2,4", "3,9"})
  void testComputeNumToSquare(Integer num, Integer square) {

      when(mathService.compute(10))
          .then(invocation -> Long.valueOf(Math.round(Math.pow(num, 2))).intValue());

      final Integer result = mathService.compute(10);

      Assertions.assertEquals(
              square,
              result
      );
      verify(mathService).compute(10); 
  }

  @MockBean(MathServiceImpl.class) 
  MathService mathService() {
      return mock(MathService.class); 
  }

}

Fazer redirect

@Get("/swaggerui")
public HttpResponse swaggerUi(){
	return HttpResponse.redirect(URI.create("/swagger-ui?docExpansion=none"));
}

Job Schedulers

@Context // make sure bean will be injected
public class JobTrigger {
	public JobTrigger(
		@Named(TaskExecutors.SCHEDULED) TaskScheduler taskScheduler
	) {
		taskScheduler.schedule("0 */30 * * * *", () -> System.println("Something!"));
	}
}

Injetar propriedades no vo com base em um properties

@Data
@NoArgsConstructor
@ConfigurationProperties(value = "mg.steampowered.api.retry")
public class SteamProps {
	private int maxAttempts;
}

GraalVM Commands Fixar boot do linux

Comments