12 dezembro 2024

Spring Boot 3.4.0 available now

 What's new in 3.4

The highlights of the 3.4 release include:

  • Structured logging
  • Support for defining additional beans of the same type as those that are auto-configured
  • Expanded virtual thread support
  • Improved support for Docker Compose and Testcontainers
  • Numerous Actuator enhancements, including info and health information for SSL certificates
  • Improved image building capabilities, including out-of-the-box support for ARM
  • Auto-configuration for MockMvcTester, an AssertJ-based alternative to MockMvc

Getting Started with Python in VS Code

About
In the “Getting Started with Python in VS Code” video, with ‪@ReynaldAdolphe‬ , viewers learn how to efficiently set up Python development environments in Visual Studio Code, including installing necessary extensions like Python and Pylance. The video demonstrates configuring Python interpreters, managing dependencies, and writing simple Python scripts.



27 agosto 2024

Sending Email Notifications with RabbitMQ Using Java (Spring Boot)



In this tutorial, you will learn the following
  • What is async programming?
  • How to Setting up rabbitmq using docker
  • Connect it with Spring boot application
  • Send email notification asynchronously
















29 maio 2024

Building a Generative AI Application with Spring AI





@Service
public class SpringAIService {

    @Autowired
    AiClient aiClient;

    @Value("${spring.ai.openai.apikey}")
    private String apiKey;

    @Value("${spring.ai.openai.imageUrl}")
    private String openAIImageUrl;


    public String getJoke(String topic){
        PromptTemplate promptTemplate = new PromptTemplate("""
                Crafting a compilation of programming jokes for my website. Would you like me to create a joke about {topic}?
                """);
        promptTemplate.add("topic", topic);
        return this.aiClient.generate(promptTemplate.create()).getGeneration().getText();
    }

    public String getBook(String category, String year) {
        PromptTemplate promptTemplate = new PromptTemplate("""
                I would like to research some books. Please give me a book about {category} in {year} to get started?
                But pick the best best you can think of. I'm a book critic. Ratings are great help.
                And who wrote it? And who help it? Can you give me a short plot summary and also it's name?
                But don't give me too much information. I don't want any spoilers.
                And please give me these details in the following JSON format: category, year, bookName, author, review, smallSummary.
                """);
        Map.of("category", category, "year", year).forEach(promptTemplate::add);
        AiResponse generate = this.aiClient.generate(promptTemplate.create());
        return generate.getGeneration().getText();
    }


    public InputStreamResource getImage(@RequestParam(name = "topic") String topic) throws URISyntaxException {
        PromptTemplate promptTemplate = new PromptTemplate("""
                 I am really bored from online memes. Can you create me a prompt about {topic}.
                 Elevate the given topic. Make it sophisticated.
                 Make a resolution of 256x256, but ensure that it is presented in json.
                 I want only one image creation. Give me as JSON format: prompt, n, size.
                """);
        promptTemplate.add("topic", topic);
        String imagePrompt = this.aiClient.generate(promptTemplate.create()).getGeneration().getText();

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + apiKey);
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(imagePrompt,headers);

        String imageUrl = restTemplate.exchange(openAIImageUrl, HttpMethod.POST, httpEntity, GeneratedImage.class)
                .getBody().getData().get(0).getUrl();
        byte[] imageBytes = restTemplate.getForObject(new URI(imageUrl), byte[].class);
        assert imageBytes != null;
        return new InputStreamResource(new java.io.ByteArrayInputStream(imageBytes));
    }
}




@RestController
@RequestMapping("/api/v1")
public class SpringAIController {
@Autowired
SpringAIService aiService;
@GetMapping("/joke")
public String getJoke(@RequestParam String topic) {
return aiService.getJoke(topic);
}
@GetMapping("/book")
public String getBook(@RequestParam(name = "category") String category, @RequestParam(name = "year") String year) {
return aiService.getBook(category, year);
}
@GetMapping(value = "/image", produces = "image/jpeg")
public ResponseEntity getImage(@RequestParam(name = "topic") String topic) throws URISyntaxException {
return ResponseEntity.ok().body(aiService.getImage(topic));
}
}






11 abril 2024

04 abril 2024

Implement JWT authentication in a Spring Boot 3 application



Prerequisites

  • JDK 11 or higher 
  • Maven 3.8 or higher 
  • MySQL running on Docker 
  • An HTTP client such as Postman, Insomnia, cURL, etc.