<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sivalokesh</title>
    <description>The latest articles on DEV Community by Sivalokesh (@sivalokesh_5).</description>
    <link>https://dev.to/sivalokesh_5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2194742%2F6c776576-179e-4365-8810-6a82ee7a3e29.png</url>
      <title>DEV Community: Sivalokesh</title>
      <link>https://dev.to/sivalokesh_5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sivalokesh_5"/>
    <language>en</language>
    <item>
      <title>Simplifying RESTful API Error Handling in Spring Boot with @ControllerAdvice</title>
      <dc:creator>Sivalokesh</dc:creator>
      <pubDate>Thu, 10 Oct 2024 16:10:25 +0000</pubDate>
      <link>https://dev.to/sivalokesh_5/simplifying-restful-api-error-handling-in-spring-boot-with-controlleradvice-3j04</link>
      <guid>https://dev.to/sivalokesh_5/simplifying-restful-api-error-handling-in-spring-boot-with-controlleradvice-3j04</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;I wanted to share a quick tip for handling errors in a more structured way in Spring Boot applications using @ControllerAdvice and @ExceptionHandler.&lt;/p&gt;

&lt;p&gt;Problem: In a typical Spring Boot REST API, we may have multiple exceptions thrown across different controllers. Manually handling them can lead to duplicated code and inconsistent responses.&lt;/p&gt;

&lt;p&gt;Solution: @ControllerAdvice provides a global error handling mechanism, allowing you to centralize exception handling logic. Here's an example:&lt;/p&gt;

&lt;p&gt;`@ControllerAdvice&lt;br&gt;
public class GlobalExceptionHandler {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity&amp;lt;ErrorResponse&amp;gt; handleResourceNotFoundException(ResourceNotFoundException ex) {
    ErrorResponse errorResponse = new ErrorResponse("RESOURCE_NOT_FOUND", ex.getMessage());
    return new ResponseEntity&amp;lt;&amp;gt;(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(Exception.class)
public ResponseEntity&amp;lt;ErrorResponse&amp;gt; handleGenericException(Exception ex) {
    ErrorResponse errorResponse = new ErrorResponse("INTERNAL_SERVER_ERROR", "An unexpected error occurred");
    return new ResponseEntity&amp;lt;&amp;gt;(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;ErrorRe&lt;code&gt;sponse Class:&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
public class ErrorResponse {&lt;br&gt;
    private String code;&lt;br&gt;
    private String message;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public ErrorResponse(String code, String message) {
    this.code = code;
    this.message = message;
}

// Getters and Setters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Key Takeaway: By using @ControllerAdvice, we can centralize error handling, making the code cleaner, more maintainable, and ensuring consistent error responses across the application.&lt;/p&gt;

&lt;p&gt;I’d love to know if anyone has other tips for simplifying error handling or improving best practices in Spring Boot!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Optimizing Stream API Usage in Java for Large Data Sets</title>
      <dc:creator>Sivalokesh</dc:creator>
      <pubDate>Thu, 10 Oct 2024 16:04:05 +0000</pubDate>
      <link>https://dev.to/sivalokesh_5/optimizing-stream-api-usage-in-java-for-large-data-sets-57jl</link>
      <guid>https://dev.to/sivalokesh_5/optimizing-stream-api-usage-in-java-for-large-data-sets-57jl</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;I wanted to share a quick optimization tip for those working with large datasets in Java using the Stream API. I recently encountered a performance bottleneck in one of my projects and found that using parallelStream() made a significant difference.&lt;/p&gt;

&lt;p&gt;Here's a basic example:&lt;/p&gt;

&lt;p&gt;`**List data = getLargeDataSet();&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;// Before: Normal stream&lt;br&gt;
List filteredData = data.stream()&lt;br&gt;
                               .filter(s -&amp;gt; s.contains("keyword"))&lt;br&gt;
                               .collect(Collectors.toList());&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;// After: Parallel stream for better performance on large datasets&lt;br&gt;
List filteredData = data.parallelStream()&lt;br&gt;
                               .filter(s -&amp;gt; s.contains("keyword"))&lt;br&gt;
                               .collect(Collectors.toList());**`&lt;/p&gt;

&lt;p&gt;By switching to parallelStream(), the processing time for filtering large datasets reduced significantly on multi-core processors. However, be cautious when using parallelStream() in scenarios where thread safety is a concern or when working with smaller data sets, as the overhead may not always justify the performance gain.&lt;/p&gt;

&lt;p&gt;I'd love to hear your thoughts or other optimization suggestions when working with Java Streams!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>java</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
