get( 'Version' ) );} );/** Preload the homepage LCP image without disabling lazy loading elsewhere. */add_action( 'wp_head', function () { if ( ! is_front_page() || is_paged() ) { return; } $posts = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish', 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', ) ); if ( empty( $posts ) ) { return; } $thumbnail_id = get_post_thumbnail_id( $posts[0]->ID ); $thumbnail_url = $thumbnail_id ? wp_get_attachment_url( $thumbnail_id ) : false; $metadata = $thumbnail_id ? wp_get_attachment_metadata( $thumbnail_id ) : array(); $size_file = ! empty( $metadata['sizes']['pixwell_370x250-2x']['file'] ) ? $metadata['sizes']['pixwell_370x250-2x']['file'] : false; $image_url = $size_file && $thumbnail_url ? trailingslashit( dirname( $thumbnail_url ) ) . $size_file : $thumbnail_url; if ( $image_url ) { printf( '' . "\n", esc_url( $image_url ) ); }}, 1 );/** Give every tag archive a concise description based on its own posts. */add_action( 'wp_head', function () { if ( ! is_tag() ) { return; } $term = get_queried_object(); if ( ! $term instanceof WP_Term ) { return; } $post_ids = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 2, 'fields' => 'ids', 'tag_id' => $term->term_id, 'orderby' => 'date', 'order' => 'DESC', ) ); $titles = array_values( array_filter( array_map( function ( $post_id ) { return wp_html_excerpt( wp_strip_all_tags( get_the_title( $post_id ) ), 42, '…' ); }, $post_ids ) ) ); $context = count( $titles ) > 1 ? sprintf( '“%1$s” ve “%2$s”', $titles[0], $titles[1] ) : ( ! empty( $titles ) ? sprintf( '“%s”', $titles[0] ) : 'ilgili teknik içerikler' ); $templates = array( '%1$s hakkında %2$s başlıklı yazılar ile teknik notları Burak Kutbay’ın kişisel blogunda keşfedin.', '%1$s etiketi altında %2$s ve konuyla bağlantılı rehberler bir arada.', 'Burak Kutbay’ın blogunda %1$s için %2$s başlıklı içerikler, deneyimler ve pratik bilgiler.', '%1$s konusuna dair %2$s; ilgili yazılar ve kaynaklar bu arşivde.', '%1$s ile ilgili %2$s ve diğer güncel teknoloji içeriklerine göz atın.', '%1$s arşivinde %2$s başlıklı içerikler ve konuya yönelik teknik açıklamalar yer alır.', ); $description = sprintf( $templates[ $term->term_id % count( $templates ) ], wp_strip_all_tags( $term->name ), $context ); printf( '' . "\n", esc_attr( wp_html_excerpt( wp_strip_all_tags( $description ), 155, '…' ) ) );}, 0 );/** Build a 140-160 character fallback for posts without a custom Yoast description. */add_filter( 'wpseo_metadesc', function ( $description ) { if ( ! is_singular( 'post' ) ) { return $description; } $post = get_queried_object(); $custom = $post instanceof WP_Post ? get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true ) : ''; if ( $custom ) { return $description; } $source = $post instanceof WP_Post && $post->post_excerpt ? $post->post_excerpt : ( $post instanceof WP_Post ? $post->post_content : '' ); $source = wp_strip_all_tags( strip_shortcodes( $source ) ); $fallback = wp_html_excerpt( $source, 155, '…' ); return $fallback ? $fallback : 'Burak Kutbay’ın kişisel blogunda Java, Spring, yazılım geliştirme ve teknoloji üzerine güncel teknik içerikleri keşfedin.';}, 20 );/** Redirect legacy slugs to their current posts without touching content. */add_action( 'template_redirect', function () { $request_path = parse_url( $_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH ); $request_path = rawurldecode( (string) $request_path ); $legacy_unicode_path = '/cocugunuza-programlama-ogretin-' . "\u{200E}" . 'programlamacocukoyuncagi' . "\u{202C}" . '.html/'; $redirects = array( '/spring-boot-transaction-isolation-propagation-verollback.html/' => '/spring-boot-transaction-isolation-propagation-ve-rollback.html/', '/testcontainers-ile-spring-boot-amp-mongodb-test-islemleri.html/' => '/testcontainers-ile-spring-boot-mongodb-test-islemleri.html/', '/google’dan-freddie-mercurye-ozel-klibi.html/' => '/googledan-freddie-mercurye-ozel-klibi.html/', $legacy_unicode_path => '/cocugunuza-programlama-ogretin-programlamacocukoyuncagi.html/', ); if ( isset( $redirects[ $request_path ] ) ) { wp_safe_redirect( home_url( $redirects[ $request_path ] ), 301 ); exit; }}, 1 ); Spring Boot Custom Hata Sayfaları Yapmak - Burak Kutbay'ın Kişisel Blogu

Arşivler

Spring Boot Custom Hata Sayfaları Yapmak

Spring Dersleri

Spring Boot Dersleri‘ne devam ediyoruz.

Spring Boot projelerimizde oluşacak hataların kullanıcılara anlamlı mesajlar halinde göstermek önemli bir husus. Default hata mesajları son kullanıcı tarafından anlaşılabilir olmadığı gibi çok tercih edilen bir durum da değil açıkcası.

Spring Boot uygulamalarımızda kendi hata sayfalarımızı yapmamız için ve Spring Boot’un kolaylıkla tanıması için aşağıdaki yolda ilgili hata sayfalarına karşılık gelen sayfalarımızın olması gerekmektedir.Resources’in altında public klasörü oluşturuyoruz ve altına error klasörü oluşturuyoruz ve ilgili sayfalarımızı bu klasörün altına koyuyoruz.

src/
 +- main/
     +- java/
     |   + ...
     +- resources/
         +- public/
             +- error/
             |   +- 404.html
             |   +- 500.html

Maven bağımlığımızla başlayalım.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Bağımlılılarımız yukarıdaki gibi olacaktır.

Hata sayfalarımızı oluşturalım.

404 hata sayfası.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>blog.burakkutbay.com</title>
</head>
<body>
    <h1>404 HATASI</h1>
    <h4>Böyle bir sayfa yok.</h4>
</body>
</html>

500 hata sayfası.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>blog.burakkutbay.com</title>
</head>
<body>
    <h1>500 HATASI</h1>
    <h4>Bir Sorun Oluştu.</h4>
</body>
</html>

Şimdi controllerimizi oluşturalım.

@RestController
public class MyController {

    @RequestMapping("/index")
    public String Index() {
        //Bilerek hata oluşturuyoruz 500 hata sayfasına düşmesi için
        int i=1/0;
        return "index";
    }
}

index adlı metodta bilerek bir hata oluşmasını sağlıyoruz.

localhost:8080/index adresine gittiğimizde 1/0 işlemi hata oluşacağı için kullanıcı 500 hatası iletilecektir. Bu 500 hatası bizim oluşturduğumuz sayfa olacaktır ve o sayfa görüntülenecektir.

500 Hatası
500 Hatası

localhost:8080 adresinde varsayılan bir sayfa oluşturmuyoruz. Çünkü 404 hatası alması istiyoruz. 404 hatası oluştuğunda bizim oluşturduğumuz sayfa gösterilmektedir.

404 Hatası
404 Hatası

Şeklinde diğer tüm hata sayfalarını oluşturabiliriz.


Önceki Ders: Spring Boot Uygulaması TimeZone Değiştirmek
Spring Boot Dersleri
Sonraki Ders:  Çoklu Veritabanı İçin Çalışmak