Posts

Showing posts with the label coding

ResourceHttpMessageConverter - Spring

Introduction In Spring Framework, ResourceHttpMessageConverter is a class that helps in converting resources into HTTP responses. It's particularly useful for serving binary files such as images, videos, or downloadable files as part of a RESTful API or web application. Here, I'll provide some examples of how to use the `ResourceHttpMessageConverter` in a Spring application. First, you need to configure Spring to use the `ResourceHttpMessageConverter` as one of the message converters in your application context configuration. You can do this in your Spring configuration XML or Java-based configuration class. 1. XML Configuration: <mvc:annotation-driven>     <mvc:message-converters>         <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />     </mvc:message-converters> </mvc:annotation-driven> 2. Java Configuration: @Configuration @EnableWebMvc public class WebConfig extends WebMvcC...

BufferedImageHttpMessageConverter Examples

Spring Framework provides several built-in HttpMessageConverters to handle different media types when working with HTTP requests and responses. The `BufferedImageHttpMessageConverter` is used to convert `BufferedImage` objects to and from HTTP requests and responses. Here are some examples of how to use it: 1. Add Dependency:    First, make sure you have the necessary dependencies in your project's build file. If you are using Maven, add the following dependency to your `pom.xml`:    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-web</artifactId>        <version>${spring.version}</version>    </dependency>    Replace `${spring.version}` with the appropriate Spring Framework version you are using. 2. Configure Spring MVC:    To use the `BufferedImageHttpMessageConverter`, you need to configur...

Streaming HttpOutputMessage Examples

Some examples of using Spring's ` Streaming HttpOutputMessage ` interface.  Example 1: Streaming a File import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.ServletServerHttpResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; @RestController public class FileStreamingController {     @GetMapping("/download")     public StreamingResponseBody downloadFile(HttpServletResponse response) throws IOException {         response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);         resp...

[Step-By-Step] How To Read a text file content in Spring Boot

Reading text file content is a common task in software development, especially when working with Spring Boot applications. In this tutorial, we will guide you through a step-by-step process to read the content of a text file using Spring Boot. Whether you're a beginner or an experienced developer, by the end of this guide, you'll have a clear understanding of how to handle text file reading in a Spring Boot environment. To read the content of a text file in a Spring Boot application, you can use the built-in utility classes provided by Java and Spring. Here's a step-by-step guide on how to do this: 1. Create the Text File: Make sure you have a text file placed in the resources directory of your Spring Boot project. For example, you can create a file named `sample.txt` inside the `src/main/resources` directory. 2. Create a Controller or Service: You can either create a controller to handle HTTP requests or a service to encapsulate the file reading logic. For this exampl...

Exploring Lambda Expressions in Python

Exploring Lambda Expressions in Python: Simplifying Your Code with Examples Introduction: Lambda expressions, also known as anonymous functions, provide a concise and powerful way to define and use functions in Python. By leveraging lambda expressions, you can simplify your code, improve readability, and enhance your programming efficiency. In this blog post, we'll dive into the world of lambda expressions in Python, explaining their syntax, demonstrating their versatility, and providing you with practical code samples. So let's get started and unlock the potential of lambda expressions! 1. Understanding Lambda Expressions: In Python, a lambda expression is an anonymous function that can take any number of arguments but can only have one expression. Its general syntax is as follows: lambda arguments: expression Lambda expressions are often used in situations where a small, one-time function is needed without the need for a formal function definition. 2. Basic Usage of Lambda Ex...

Spring Boot - Handling Exceptions In REST

Image
In this post, we will see how to handle exceptions using Custom Exception and send error response in REST apis. Here we read error messages from properties file , using Message Bundle. Project Structure: Spring Boot - Handling Exceptions In REST pom.xml <? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0 . 0 </ modelVersion > < parent > < groupId > org . springframework . boot </ groupId > < artifactId > spring - boot - starter - parent </ artifactId > < version > 2.7 . 11 </ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId > com . javainspires...

Java Program To Check for an element in Array

Image
Using For Loop: package com . javainspires . examples ; import java.util.Arrays ; import java.util.List ; import java.util.stream.Stream ; public class ExampleMain { public static void main ( String [] args ) { //take a string array String [] names = { "Java" , "Python" , "Kotlin" , "Go" }; String toFind = "Go" ; // using for loop for ( String name : names ) { if ( toFind . equals ( name )){ System . out . println ( "Given array conatins " + toFind ); } } } } Output: Given array conatins Go Using List Contains method: package com . javainspires . examples ; import java.util.Arrays ; import java.util.List ; import java.util.stream.Stream ; public class ExampleMain { public static void main ( String [] args ) { //take a string array String [] nam...