[{"data":1,"prerenderedAt":4115},["ShallowReactive",2],{"post-pattern-matching-sealed-classes-java21":3,"rel-pattern-matching-sealed-classes-java21":918,"sib-pattern-matching-sealed-classes-java21":4043},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":12,"image":13,"optimized_image":13,"category":14,"tags":15,"author":20,"paginate":6,"body":21,"_type":912,"_id":913,"_source":914,"_file":915,"_stem":916,"_extension":917},"/posts/pattern-matching-sealed-classes-java21","posts",false,"","A Guide to Pattern Matching and Sealed Classes in Java 21","A comprehensive introduction to pattern matching and sealed classes in Java 21, including practical examples and best practices.","2024-05-15T11:00:00.000Z","post","Writing Safer and More Expressive Java Code","/assets/img/uploads/java-pattern.matching.png","code",[14,16,17,18,19],"java","pattern-matching","sealed-classes","java21","jaimedearcos",{"type":22,"children":23,"toc":898},"root",[24,51,58,71,78,207,213,385,391,411,416,421,427,613,619,802,807,825,831,849,855,860,867,892],{"type":25,"tag":26,"props":27,"children":28},"element","blockquote",{},[29],{"type":25,"tag":30,"props":31,"children":32},"p",{},[33,36,42,44,49],{"type":34,"value":35},"text","Java 21 introduces powerful enhancements that improve readability, safety, and expressiveness in your code. Among these, ",{"type":25,"tag":37,"props":38,"children":39},"strong",{},[40],{"type":34,"value":41},"pattern matching",{"type":34,"value":43}," and ",{"type":25,"tag":37,"props":45,"children":46},{},[47],{"type":34,"value":48},"sealed classes",{"type":34,"value":50}," stand out, allowing developers to write concise, readable, and safe code. Let's explore both concepts with practical examples.",{"type":25,"tag":52,"props":53,"children":55},"h2",{"id":54},"pattern-matching-for-switch",[56],{"type":34,"value":57},"Pattern Matching for Switch",{"type":25,"tag":30,"props":59,"children":60},{},[61,63,69],{"type":34,"value":62},"Pattern matching extends the classic ",{"type":25,"tag":14,"props":64,"children":66},{"className":65},[],[67],{"type":34,"value":68},"switch",{"type":34,"value":70}," statement, making code simpler and clearer by reducing the need for explicit casting and type checking.",{"type":25,"tag":72,"props":73,"children":75},"h3",{"id":74},"traditional-switch",[76],{"type":34,"value":77},"Traditional Switch",{"type":25,"tag":79,"props":80,"children":83},"pre",{"className":81,"code":82,"language":16,"meta":7,"style":7},"language-java shiki shiki-themes github-dark github-light","Object obj = \"Hello\";\n\nif (obj instanceof String) {\n    String s = (String) obj;\n    System.out.println(s.toLowerCase());\n}\n",[84],{"type":25,"tag":14,"props":85,"children":86},{"__ignoreMap":7},[87,116,126,150,168,198],{"type":25,"tag":88,"props":89,"children":92},"span",{"class":90,"line":91},"line",1,[93,99,105,111],{"type":25,"tag":88,"props":94,"children":96},{"style":95},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[97],{"type":34,"value":98},"Object obj ",{"type":25,"tag":88,"props":100,"children":102},{"style":101},"--shiki-default:#F97583;--shiki-light:#D73A49",[103],{"type":34,"value":104},"=",{"type":25,"tag":88,"props":106,"children":108},{"style":107},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[109],{"type":34,"value":110}," \"Hello\"",{"type":25,"tag":88,"props":112,"children":113},{"style":95},[114],{"type":34,"value":115},";\n",{"type":25,"tag":88,"props":117,"children":119},{"class":90,"line":118},2,[120],{"type":25,"tag":88,"props":121,"children":123},{"emptyLinePlaceholder":122},true,[124],{"type":34,"value":125},"\n",{"type":25,"tag":88,"props":127,"children":129},{"class":90,"line":128},3,[130,135,140,145],{"type":25,"tag":88,"props":131,"children":132},{"style":101},[133],{"type":34,"value":134},"if",{"type":25,"tag":88,"props":136,"children":137},{"style":95},[138],{"type":34,"value":139}," (obj ",{"type":25,"tag":88,"props":141,"children":142},{"style":101},[143],{"type":34,"value":144},"instanceof",{"type":25,"tag":88,"props":146,"children":147},{"style":95},[148],{"type":34,"value":149}," String) {\n",{"type":25,"tag":88,"props":151,"children":153},{"class":90,"line":152},4,[154,159,163],{"type":25,"tag":88,"props":155,"children":156},{"style":95},[157],{"type":34,"value":158},"    String s ",{"type":25,"tag":88,"props":160,"children":161},{"style":101},[162],{"type":34,"value":104},{"type":25,"tag":88,"props":164,"children":165},{"style":95},[166],{"type":34,"value":167}," (String) obj;\n",{"type":25,"tag":88,"props":169,"children":171},{"class":90,"line":170},5,[172,177,183,188,193],{"type":25,"tag":88,"props":173,"children":174},{"style":95},[175],{"type":34,"value":176},"    System.out.",{"type":25,"tag":88,"props":178,"children":180},{"style":179},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[181],{"type":34,"value":182},"println",{"type":25,"tag":88,"props":184,"children":185},{"style":95},[186],{"type":34,"value":187},"(s.",{"type":25,"tag":88,"props":189,"children":190},{"style":179},[191],{"type":34,"value":192},"toLowerCase",{"type":25,"tag":88,"props":194,"children":195},{"style":95},[196],{"type":34,"value":197},"());\n",{"type":25,"tag":88,"props":199,"children":201},{"class":90,"line":200},6,[202],{"type":25,"tag":88,"props":203,"children":204},{"style":95},[205],{"type":34,"value":206},"}\n",{"type":25,"tag":72,"props":208,"children":210},{"id":209},"pattern-matching-switch",[211],{"type":34,"value":212},"Pattern Matching Switch",{"type":25,"tag":79,"props":214,"children":216},{"className":81,"code":215,"language":16,"meta":7,"style":7},"Object obj = \"Hello\";\n\nswitch (obj) {\n    case String s -> System.out.println(s.toLowerCase());\n    case Integer i -> System.out.println(i + 1);\n    default -> System.out.println(\"Unknown type\");\n}\n",[217],{"type":25,"tag":14,"props":218,"children":219},{"__ignoreMap":7},[220,239,246,258,297,342,377],{"type":25,"tag":88,"props":221,"children":222},{"class":90,"line":91},[223,227,231,235],{"type":25,"tag":88,"props":224,"children":225},{"style":95},[226],{"type":34,"value":98},{"type":25,"tag":88,"props":228,"children":229},{"style":101},[230],{"type":34,"value":104},{"type":25,"tag":88,"props":232,"children":233},{"style":107},[234],{"type":34,"value":110},{"type":25,"tag":88,"props":236,"children":237},{"style":95},[238],{"type":34,"value":115},{"type":25,"tag":88,"props":240,"children":241},{"class":90,"line":118},[242],{"type":25,"tag":88,"props":243,"children":244},{"emptyLinePlaceholder":122},[245],{"type":34,"value":125},{"type":25,"tag":88,"props":247,"children":248},{"class":90,"line":128},[249,253],{"type":25,"tag":88,"props":250,"children":251},{"style":101},[252],{"type":34,"value":68},{"type":25,"tag":88,"props":254,"children":255},{"style":95},[256],{"type":34,"value":257}," (obj) {\n",{"type":25,"tag":88,"props":259,"children":260},{"class":90,"line":152},[261,266,271,276,281,285,289,293],{"type":25,"tag":88,"props":262,"children":263},{"style":101},[264],{"type":34,"value":265},"    case",{"type":25,"tag":88,"props":267,"children":268},{"style":95},[269],{"type":34,"value":270}," String s ",{"type":25,"tag":88,"props":272,"children":273},{"style":101},[274],{"type":34,"value":275},"->",{"type":25,"tag":88,"props":277,"children":278},{"style":95},[279],{"type":34,"value":280}," System.out.",{"type":25,"tag":88,"props":282,"children":283},{"style":179},[284],{"type":34,"value":182},{"type":25,"tag":88,"props":286,"children":287},{"style":95},[288],{"type":34,"value":187},{"type":25,"tag":88,"props":290,"children":291},{"style":179},[292],{"type":34,"value":192},{"type":25,"tag":88,"props":294,"children":295},{"style":95},[296],{"type":34,"value":197},{"type":25,"tag":88,"props":298,"children":299},{"class":90,"line":170},[300,304,309,313,317,321,326,331,337],{"type":25,"tag":88,"props":301,"children":302},{"style":101},[303],{"type":34,"value":265},{"type":25,"tag":88,"props":305,"children":306},{"style":95},[307],{"type":34,"value":308}," Integer i ",{"type":25,"tag":88,"props":310,"children":311},{"style":101},[312],{"type":34,"value":275},{"type":25,"tag":88,"props":314,"children":315},{"style":95},[316],{"type":34,"value":280},{"type":25,"tag":88,"props":318,"children":319},{"style":179},[320],{"type":34,"value":182},{"type":25,"tag":88,"props":322,"children":323},{"style":95},[324],{"type":34,"value":325},"(i ",{"type":25,"tag":88,"props":327,"children":328},{"style":101},[329],{"type":34,"value":330},"+",{"type":25,"tag":88,"props":332,"children":334},{"style":333},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[335],{"type":34,"value":336}," 1",{"type":25,"tag":88,"props":338,"children":339},{"style":95},[340],{"type":34,"value":341},");\n",{"type":25,"tag":88,"props":343,"children":344},{"class":90,"line":200},[345,350,355,359,363,368,373],{"type":25,"tag":88,"props":346,"children":347},{"style":101},[348],{"type":34,"value":349},"    default",{"type":25,"tag":88,"props":351,"children":352},{"style":101},[353],{"type":34,"value":354}," ->",{"type":25,"tag":88,"props":356,"children":357},{"style":95},[358],{"type":34,"value":280},{"type":25,"tag":88,"props":360,"children":361},{"style":179},[362],{"type":34,"value":182},{"type":25,"tag":88,"props":364,"children":365},{"style":95},[366],{"type":34,"value":367},"(",{"type":25,"tag":88,"props":369,"children":370},{"style":107},[371],{"type":34,"value":372},"\"Unknown type\"",{"type":25,"tag":88,"props":374,"children":375},{"style":95},[376],{"type":34,"value":341},{"type":25,"tag":88,"props":378,"children":380},{"class":90,"line":379},7,[381],{"type":25,"tag":88,"props":382,"children":383},{"style":95},[384],{"type":34,"value":206},{"type":25,"tag":72,"props":386,"children":388},{"id":387},"benefits",[389],{"type":34,"value":390},"Benefits:",{"type":25,"tag":392,"props":393,"children":394},"ul",{},[395,401,406],{"type":25,"tag":396,"props":397,"children":398},"li",{},[399],{"type":34,"value":400},"Reduced boilerplate code",{"type":25,"tag":396,"props":402,"children":403},{},[404],{"type":34,"value":405},"Enhanced readability",{"type":25,"tag":396,"props":407,"children":408},{},[409],{"type":34,"value":410},"Improved type safety",{"type":25,"tag":52,"props":412,"children":413},{"id":18},[414],{"type":34,"value":415},"Sealed Classes",{"type":25,"tag":30,"props":417,"children":418},{},[419],{"type":34,"value":420},"Sealed classes allow you to define which subclasses or implementations are permitted. This helps in modeling clear and controlled domain hierarchies.",{"type":25,"tag":72,"props":422,"children":424},{"id":423},"declaring-a-sealed-class",[425],{"type":34,"value":426},"Declaring a Sealed Class",{"type":25,"tag":79,"props":428,"children":430},{"className":81,"code":429,"language":16,"meta":7,"style":7},"public sealed abstract class Shape\n    permits Circle, Rectangle {}\n\npublic final class Circle extends Shape {\n    double radius;\n}\n\npublic final class Rectangle extends Shape {\n    double length, width;\n}\n",[431],{"type":25,"tag":14,"props":432,"children":433},{"__ignoreMap":7},[434,462,490,497,532,545,552,559,592,605],{"type":25,"tag":88,"props":435,"children":436},{"class":90,"line":91},[437,442,447,452,457],{"type":25,"tag":88,"props":438,"children":439},{"style":101},[440],{"type":34,"value":441},"public",{"type":25,"tag":88,"props":443,"children":444},{"style":101},[445],{"type":34,"value":446}," sealed",{"type":25,"tag":88,"props":448,"children":449},{"style":101},[450],{"type":34,"value":451}," abstract",{"type":25,"tag":88,"props":453,"children":454},{"style":101},[455],{"type":34,"value":456}," class",{"type":25,"tag":88,"props":458,"children":459},{"style":179},[460],{"type":34,"value":461}," Shape\n",{"type":25,"tag":88,"props":463,"children":464},{"class":90,"line":118},[465,470,475,480,485],{"type":25,"tag":88,"props":466,"children":467},{"style":101},[468],{"type":34,"value":469},"    permits",{"type":25,"tag":88,"props":471,"children":472},{"style":179},[473],{"type":34,"value":474}," Circle",{"type":25,"tag":88,"props":476,"children":477},{"style":95},[478],{"type":34,"value":479},", ",{"type":25,"tag":88,"props":481,"children":482},{"style":179},[483],{"type":34,"value":484},"Rectangle",{"type":25,"tag":88,"props":486,"children":487},{"style":95},[488],{"type":34,"value":489}," {}\n",{"type":25,"tag":88,"props":491,"children":492},{"class":90,"line":128},[493],{"type":25,"tag":88,"props":494,"children":495},{"emptyLinePlaceholder":122},[496],{"type":34,"value":125},{"type":25,"tag":88,"props":498,"children":499},{"class":90,"line":152},[500,504,509,513,517,522,527],{"type":25,"tag":88,"props":501,"children":502},{"style":101},[503],{"type":34,"value":441},{"type":25,"tag":88,"props":505,"children":506},{"style":101},[507],{"type":34,"value":508}," final",{"type":25,"tag":88,"props":510,"children":511},{"style":101},[512],{"type":34,"value":456},{"type":25,"tag":88,"props":514,"children":515},{"style":179},[516],{"type":34,"value":474},{"type":25,"tag":88,"props":518,"children":519},{"style":101},[520],{"type":34,"value":521}," extends",{"type":25,"tag":88,"props":523,"children":524},{"style":179},[525],{"type":34,"value":526}," Shape",{"type":25,"tag":88,"props":528,"children":529},{"style":95},[530],{"type":34,"value":531}," {\n",{"type":25,"tag":88,"props":533,"children":534},{"class":90,"line":170},[535,540],{"type":25,"tag":88,"props":536,"children":537},{"style":101},[538],{"type":34,"value":539},"    double",{"type":25,"tag":88,"props":541,"children":542},{"style":95},[543],{"type":34,"value":544}," radius;\n",{"type":25,"tag":88,"props":546,"children":547},{"class":90,"line":200},[548],{"type":25,"tag":88,"props":549,"children":550},{"style":95},[551],{"type":34,"value":206},{"type":25,"tag":88,"props":553,"children":554},{"class":90,"line":379},[555],{"type":25,"tag":88,"props":556,"children":557},{"emptyLinePlaceholder":122},[558],{"type":34,"value":125},{"type":25,"tag":88,"props":560,"children":562},{"class":90,"line":561},8,[563,567,571,575,580,584,588],{"type":25,"tag":88,"props":564,"children":565},{"style":101},[566],{"type":34,"value":441},{"type":25,"tag":88,"props":568,"children":569},{"style":101},[570],{"type":34,"value":508},{"type":25,"tag":88,"props":572,"children":573},{"style":101},[574],{"type":34,"value":456},{"type":25,"tag":88,"props":576,"children":577},{"style":179},[578],{"type":34,"value":579}," Rectangle",{"type":25,"tag":88,"props":581,"children":582},{"style":101},[583],{"type":34,"value":521},{"type":25,"tag":88,"props":585,"children":586},{"style":179},[587],{"type":34,"value":526},{"type":25,"tag":88,"props":589,"children":590},{"style":95},[591],{"type":34,"value":531},{"type":25,"tag":88,"props":593,"children":595},{"class":90,"line":594},9,[596,600],{"type":25,"tag":88,"props":597,"children":598},{"style":101},[599],{"type":34,"value":539},{"type":25,"tag":88,"props":601,"children":602},{"style":95},[603],{"type":34,"value":604}," length, width;\n",{"type":25,"tag":88,"props":606,"children":608},{"class":90,"line":607},10,[609],{"type":25,"tag":88,"props":610,"children":611},{"style":95},[612],{"type":34,"value":206},{"type":25,"tag":72,"props":614,"children":616},{"id":615},"using-pattern-matching-with-sealed-classes",[617],{"type":34,"value":618},"Using Pattern Matching with Sealed Classes",{"type":25,"tag":79,"props":620,"children":622},{"className":81,"code":621,"language":16,"meta":7,"style":7},"Shape shape = new Circle();\n\ndouble area = switch (shape) {\n    case Circle c -> Math.PI * c.radius * c.radius;\n    case Rectangle r -> r.length * r.width;\n};\n\nSystem.out.println(\"Area: \" + area);\n",[623],{"type":25,"tag":14,"props":624,"children":625},{"__ignoreMap":7},[626,652,659,686,726,756,764,771],{"type":25,"tag":88,"props":627,"children":628},{"class":90,"line":91},[629,634,638,643,647],{"type":25,"tag":88,"props":630,"children":631},{"style":95},[632],{"type":34,"value":633},"Shape shape ",{"type":25,"tag":88,"props":635,"children":636},{"style":101},[637],{"type":34,"value":104},{"type":25,"tag":88,"props":639,"children":640},{"style":101},[641],{"type":34,"value":642}," new",{"type":25,"tag":88,"props":644,"children":645},{"style":179},[646],{"type":34,"value":474},{"type":25,"tag":88,"props":648,"children":649},{"style":95},[650],{"type":34,"value":651},"();\n",{"type":25,"tag":88,"props":653,"children":654},{"class":90,"line":118},[655],{"type":25,"tag":88,"props":656,"children":657},{"emptyLinePlaceholder":122},[658],{"type":34,"value":125},{"type":25,"tag":88,"props":660,"children":661},{"class":90,"line":128},[662,667,672,676,681],{"type":25,"tag":88,"props":663,"children":664},{"style":101},[665],{"type":34,"value":666},"double",{"type":25,"tag":88,"props":668,"children":669},{"style":95},[670],{"type":34,"value":671}," area ",{"type":25,"tag":88,"props":673,"children":674},{"style":101},[675],{"type":34,"value":104},{"type":25,"tag":88,"props":677,"children":678},{"style":101},[679],{"type":34,"value":680}," switch",{"type":25,"tag":88,"props":682,"children":683},{"style":95},[684],{"type":34,"value":685}," (shape) {\n",{"type":25,"tag":88,"props":687,"children":688},{"class":90,"line":152},[689,693,698,702,707,712,717,721],{"type":25,"tag":88,"props":690,"children":691},{"style":101},[692],{"type":34,"value":265},{"type":25,"tag":88,"props":694,"children":695},{"style":95},[696],{"type":34,"value":697}," Circle c ",{"type":25,"tag":88,"props":699,"children":700},{"style":101},[701],{"type":34,"value":275},{"type":25,"tag":88,"props":703,"children":704},{"style":95},[705],{"type":34,"value":706}," Math.PI ",{"type":25,"tag":88,"props":708,"children":709},{"style":101},[710],{"type":34,"value":711},"*",{"type":25,"tag":88,"props":713,"children":714},{"style":95},[715],{"type":34,"value":716}," c.radius ",{"type":25,"tag":88,"props":718,"children":719},{"style":101},[720],{"type":34,"value":711},{"type":25,"tag":88,"props":722,"children":723},{"style":95},[724],{"type":34,"value":725}," c.radius;\n",{"type":25,"tag":88,"props":727,"children":728},{"class":90,"line":170},[729,733,738,742,747,751],{"type":25,"tag":88,"props":730,"children":731},{"style":101},[732],{"type":34,"value":265},{"type":25,"tag":88,"props":734,"children":735},{"style":95},[736],{"type":34,"value":737}," Rectangle r ",{"type":25,"tag":88,"props":739,"children":740},{"style":101},[741],{"type":34,"value":275},{"type":25,"tag":88,"props":743,"children":744},{"style":95},[745],{"type":34,"value":746}," r.length ",{"type":25,"tag":88,"props":748,"children":749},{"style":101},[750],{"type":34,"value":711},{"type":25,"tag":88,"props":752,"children":753},{"style":95},[754],{"type":34,"value":755}," r.width;\n",{"type":25,"tag":88,"props":757,"children":758},{"class":90,"line":200},[759],{"type":25,"tag":88,"props":760,"children":761},{"style":95},[762],{"type":34,"value":763},"};\n",{"type":25,"tag":88,"props":765,"children":766},{"class":90,"line":379},[767],{"type":25,"tag":88,"props":768,"children":769},{"emptyLinePlaceholder":122},[770],{"type":34,"value":125},{"type":25,"tag":88,"props":772,"children":773},{"class":90,"line":561},[774,779,783,787,792,797],{"type":25,"tag":88,"props":775,"children":776},{"style":95},[777],{"type":34,"value":778},"System.out.",{"type":25,"tag":88,"props":780,"children":781},{"style":179},[782],{"type":34,"value":182},{"type":25,"tag":88,"props":784,"children":785},{"style":95},[786],{"type":34,"value":367},{"type":25,"tag":88,"props":788,"children":789},{"style":107},[790],{"type":34,"value":791},"\"Area: \"",{"type":25,"tag":88,"props":793,"children":794},{"style":101},[795],{"type":34,"value":796}," +",{"type":25,"tag":88,"props":798,"children":799},{"style":95},[800],{"type":34,"value":801}," area);\n",{"type":25,"tag":72,"props":803,"children":805},{"id":804},"benefits-1",[806],{"type":34,"value":390},{"type":25,"tag":392,"props":808,"children":809},{},[810,815,820],{"type":25,"tag":396,"props":811,"children":812},{},[813],{"type":34,"value":814},"Enhanced clarity of inheritance hierarchies",{"type":25,"tag":396,"props":816,"children":817},{},[818],{"type":34,"value":819},"Compile-time enforcement of permitted subclasses",{"type":25,"tag":396,"props":821,"children":822},{},[823],{"type":34,"value":824},"Simplified, safer code when used with pattern matching",{"type":25,"tag":52,"props":826,"children":828},{"id":827},"practical-applications",[829],{"type":34,"value":830},"Practical Applications",{"type":25,"tag":392,"props":832,"children":833},{},[834,839,844],{"type":25,"tag":396,"props":835,"children":836},{},[837],{"type":34,"value":838},"Domain modeling with restricted class hierarchies",{"type":25,"tag":396,"props":840,"children":841},{},[842],{"type":34,"value":843},"Type-safe parsing and processing of structured data",{"type":25,"tag":396,"props":845,"children":846},{},[847],{"type":34,"value":848},"Cleaner implementations of polymorphic behavior",{"type":25,"tag":52,"props":850,"children":852},{"id":851},"conclusion",[853],{"type":34,"value":854},"Conclusion",{"type":25,"tag":30,"props":856,"children":857},{},[858],{"type":34,"value":859},"Pattern matching and sealed classes provide significant improvements in Java 21, helping you write cleaner, safer, and more maintainable code. Mastering these tools allows developers to leverage the full expressive power of modern Java.",{"type":25,"tag":861,"props":862,"children":864},"h1",{"id":863},"further-reading",[865],{"type":34,"value":866},"Further Reading",{"type":25,"tag":392,"props":868,"children":869},{},[870,882],{"type":25,"tag":396,"props":871,"children":872},{},[873],{"type":25,"tag":874,"props":875,"children":879},"a",{"href":876,"rel":877},"https://openjdk.org/jeps/441",[878],"nofollow",[880],{"type":34,"value":881},"Java 21 Pattern Matching JEP",{"type":25,"tag":396,"props":883,"children":884},{},[885],{"type":25,"tag":874,"props":886,"children":889},{"href":887,"rel":888},"https://openjdk.org/jeps/409",[878],[890],{"type":34,"value":891},"Java 21 Sealed Classes JEP",{"type":25,"tag":893,"props":894,"children":895},"style",{},[896],{"type":34,"value":897},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":118,"depth":118,"links":899},[900,905,910,911],{"id":54,"depth":118,"text":57,"children":901},[902,903,904],{"id":74,"depth":128,"text":77},{"id":209,"depth":128,"text":212},{"id":387,"depth":128,"text":390},{"id":18,"depth":118,"text":415,"children":906},[907,908,909],{"id":423,"depth":128,"text":426},{"id":615,"depth":128,"text":618},{"id":804,"depth":128,"text":390},{"id":827,"depth":118,"text":830},{"id":851,"depth":118,"text":854},"markdown","content:posts:pattern-matching-sealed-classes-java21.md","content","posts/pattern-matching-sealed-classes-java21.md","posts/pattern-matching-sealed-classes-java21","md",[919,2331,3229],{"_path":920,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":921,"description":922,"date":923,"layout":11,"subtitle":924,"image":925,"optimized_image":925,"category":14,"tags":926,"author":20,"paginate":6,"body":932,"_type":912,"_id":2328,"_source":914,"_file":2329,"_stem":2330,"_extension":917},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png",[14,927,928,929,930,931],"gpt","openai","sql","ai","nlp",{"type":22,"children":933,"toc":2317},[934,942,948,966,972,977,1019,1032,1048,1054,1059,1270,1276,1281,1617,1623,1628,1978,1984,1989,2048,2054,2059,2239,2245,2263,2269,2274,2280,2313],{"type":25,"tag":26,"props":935,"children":936},{},[937],{"type":25,"tag":30,"props":938,"children":939},{},[940],{"type":34,"value":941},"Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":25,"tag":52,"props":943,"children":945},{"id":944},"requisitos-previos",[946],{"type":34,"value":947},"Requisitos Previos",{"type":25,"tag":392,"props":949,"children":950},{},[951,956,961],{"type":25,"tag":396,"props":952,"children":953},{},[954],{"type":34,"value":955},"Cuenta y clave API de OpenAI",{"type":25,"tag":396,"props":957,"children":958},{},[959],{"type":34,"value":960},"Python instalado en tu entorno",{"type":25,"tag":396,"props":962,"children":963},{},[964],{"type":34,"value":965},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":25,"tag":52,"props":967,"children":969},{"id":968},"paso-1-configuración-del-entorno",[970],{"type":34,"value":971},"Paso 1: Configuración del entorno",{"type":25,"tag":30,"props":973,"children":974},{},[975],{"type":34,"value":976},"Primero, instala las dependencias necesarias:",{"type":25,"tag":79,"props":978,"children":982},{"className":979,"code":980,"language":981,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","pip install openai python-dotenv sqlalchemy psycopg2-binary\n","bash",[983],{"type":25,"tag":14,"props":984,"children":985},{"__ignoreMap":7},[986],{"type":25,"tag":88,"props":987,"children":988},{"class":90,"line":91},[989,994,999,1004,1009,1014],{"type":25,"tag":88,"props":990,"children":991},{"style":179},[992],{"type":34,"value":993},"pip",{"type":25,"tag":88,"props":995,"children":996},{"style":107},[997],{"type":34,"value":998}," install",{"type":25,"tag":88,"props":1000,"children":1001},{"style":107},[1002],{"type":34,"value":1003}," openai",{"type":25,"tag":88,"props":1005,"children":1006},{"style":107},[1007],{"type":34,"value":1008}," python-dotenv",{"type":25,"tag":88,"props":1010,"children":1011},{"style":107},[1012],{"type":34,"value":1013}," sqlalchemy",{"type":25,"tag":88,"props":1015,"children":1016},{"style":107},[1017],{"type":34,"value":1018}," psycopg2-binary\n",{"type":25,"tag":30,"props":1020,"children":1021},{},[1022,1024,1030],{"type":34,"value":1023},"Guarda tu clave API en un archivo ",{"type":25,"tag":14,"props":1025,"children":1027},{"className":1026},[],[1028],{"type":34,"value":1029},".env",{"type":34,"value":1031},":",{"type":25,"tag":79,"props":1033,"children":1037},{"className":1034,"code":1035,"language":1036,"meta":7,"style":7},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[1038],{"type":25,"tag":14,"props":1039,"children":1040},{"__ignoreMap":7},[1041],{"type":25,"tag":88,"props":1042,"children":1043},{"class":90,"line":91},[1044],{"type":25,"tag":88,"props":1045,"children":1046},{},[1047],{"type":34,"value":1035},{"type":25,"tag":52,"props":1049,"children":1051},{"id":1050},"paso-2-conexión-a-la-base-de-datos",[1052],{"type":34,"value":1053},"Paso 2: Conexión a la base de datos",{"type":25,"tag":30,"props":1055,"children":1056},{},[1057],{"type":34,"value":1058},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":25,"tag":79,"props":1060,"children":1064},{"className":1061,"code":1062,"language":1063,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n","python",[1065],{"type":25,"tag":14,"props":1066,"children":1067},{"__ignoreMap":7},[1068,1091,1112,1124,1145,1152,1160,1167,1195,1220,1252],{"type":25,"tag":88,"props":1069,"children":1070},{"class":90,"line":91},[1071,1076,1081,1086],{"type":25,"tag":88,"props":1072,"children":1073},{"style":101},[1074],{"type":34,"value":1075},"from",{"type":25,"tag":88,"props":1077,"children":1078},{"style":95},[1079],{"type":34,"value":1080}," sqlalchemy ",{"type":25,"tag":88,"props":1082,"children":1083},{"style":101},[1084],{"type":34,"value":1085},"import",{"type":25,"tag":88,"props":1087,"children":1088},{"style":95},[1089],{"type":34,"value":1090}," create_engine\n",{"type":25,"tag":88,"props":1092,"children":1093},{"class":90,"line":118},[1094,1098,1103,1107],{"type":25,"tag":88,"props":1095,"children":1096},{"style":101},[1097],{"type":34,"value":1075},{"type":25,"tag":88,"props":1099,"children":1100},{"style":95},[1101],{"type":34,"value":1102}," sqlalchemy.orm ",{"type":25,"tag":88,"props":1104,"children":1105},{"style":101},[1106],{"type":34,"value":1085},{"type":25,"tag":88,"props":1108,"children":1109},{"style":95},[1110],{"type":34,"value":1111}," sessionmaker\n",{"type":25,"tag":88,"props":1113,"children":1114},{"class":90,"line":128},[1115,1119],{"type":25,"tag":88,"props":1116,"children":1117},{"style":101},[1118],{"type":34,"value":1085},{"type":25,"tag":88,"props":1120,"children":1121},{"style":95},[1122],{"type":34,"value":1123}," os\n",{"type":25,"tag":88,"props":1125,"children":1126},{"class":90,"line":152},[1127,1131,1136,1140],{"type":25,"tag":88,"props":1128,"children":1129},{"style":101},[1130],{"type":34,"value":1075},{"type":25,"tag":88,"props":1132,"children":1133},{"style":95},[1134],{"type":34,"value":1135}," dotenv ",{"type":25,"tag":88,"props":1137,"children":1138},{"style":101},[1139],{"type":34,"value":1085},{"type":25,"tag":88,"props":1141,"children":1142},{"style":95},[1143],{"type":34,"value":1144}," load_dotenv\n",{"type":25,"tag":88,"props":1146,"children":1147},{"class":90,"line":170},[1148],{"type":25,"tag":88,"props":1149,"children":1150},{"emptyLinePlaceholder":122},[1151],{"type":34,"value":125},{"type":25,"tag":88,"props":1153,"children":1154},{"class":90,"line":200},[1155],{"type":25,"tag":88,"props":1156,"children":1157},{"style":95},[1158],{"type":34,"value":1159},"load_dotenv()\n",{"type":25,"tag":88,"props":1161,"children":1162},{"class":90,"line":379},[1163],{"type":25,"tag":88,"props":1164,"children":1165},{"emptyLinePlaceholder":122},[1166],{"type":34,"value":125},{"type":25,"tag":88,"props":1168,"children":1169},{"class":90,"line":561},[1170,1175,1180,1185,1190],{"type":25,"tag":88,"props":1171,"children":1172},{"style":333},[1173],{"type":34,"value":1174},"DATABASE_URL",{"type":25,"tag":88,"props":1176,"children":1177},{"style":101},[1178],{"type":34,"value":1179}," =",{"type":25,"tag":88,"props":1181,"children":1182},{"style":95},[1183],{"type":34,"value":1184}," os.getenv(",{"type":25,"tag":88,"props":1186,"children":1187},{"style":107},[1188],{"type":34,"value":1189},"\"DATABASE_URL\"",{"type":25,"tag":88,"props":1191,"children":1192},{"style":95},[1193],{"type":34,"value":1194},")\n",{"type":25,"tag":88,"props":1196,"children":1197},{"class":90,"line":594},[1198,1203,1207,1212,1216],{"type":25,"tag":88,"props":1199,"children":1200},{"style":95},[1201],{"type":34,"value":1202},"engine ",{"type":25,"tag":88,"props":1204,"children":1205},{"style":101},[1206],{"type":34,"value":104},{"type":25,"tag":88,"props":1208,"children":1209},{"style":95},[1210],{"type":34,"value":1211}," create_engine(",{"type":25,"tag":88,"props":1213,"children":1214},{"style":333},[1215],{"type":34,"value":1174},{"type":25,"tag":88,"props":1217,"children":1218},{"style":95},[1219],{"type":34,"value":1194},{"type":25,"tag":88,"props":1221,"children":1222},{"class":90,"line":607},[1223,1228,1232,1237,1243,1247],{"type":25,"tag":88,"props":1224,"children":1225},{"style":95},[1226],{"type":34,"value":1227},"Session ",{"type":25,"tag":88,"props":1229,"children":1230},{"style":101},[1231],{"type":34,"value":104},{"type":25,"tag":88,"props":1233,"children":1234},{"style":95},[1235],{"type":34,"value":1236}," sessionmaker(",{"type":25,"tag":88,"props":1238,"children":1240},{"style":1239},"--shiki-default:#FFAB70;--shiki-light:#E36209",[1241],{"type":34,"value":1242},"bind",{"type":25,"tag":88,"props":1244,"children":1245},{"style":101},[1246],{"type":34,"value":104},{"type":25,"tag":88,"props":1248,"children":1249},{"style":95},[1250],{"type":34,"value":1251},"engine)\n",{"type":25,"tag":88,"props":1253,"children":1255},{"class":90,"line":1254},11,[1256,1261,1265],{"type":25,"tag":88,"props":1257,"children":1258},{"style":95},[1259],{"type":34,"value":1260},"session ",{"type":25,"tag":88,"props":1262,"children":1263},{"style":101},[1264],{"type":34,"value":104},{"type":25,"tag":88,"props":1266,"children":1267},{"style":95},[1268],{"type":34,"value":1269}," Session()\n",{"type":25,"tag":52,"props":1271,"children":1273},{"id":1272},"paso-3-obtener-esquema-de-la-base-de-datos",[1274],{"type":34,"value":1275},"Paso 3: Obtener esquema de la base de datos",{"type":25,"tag":30,"props":1277,"children":1278},{},[1279],{"type":34,"value":1280},"Genera una descripción del esquema que será utilizada por GPT:",{"type":25,"tag":79,"props":1282,"children":1284},{"className":1061,"code":1283,"language":1063,"meta":7,"style":7},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[1285],{"type":25,"tag":14,"props":1286,"children":1287},{"__ignoreMap":7},[1288,1308,1315,1333,1350,1367,1390,1433,1455,1540,1564,1577,1585,1603],{"type":25,"tag":88,"props":1289,"children":1290},{"class":90,"line":91},[1291,1295,1299,1303],{"type":25,"tag":88,"props":1292,"children":1293},{"style":101},[1294],{"type":34,"value":1075},{"type":25,"tag":88,"props":1296,"children":1297},{"style":95},[1298],{"type":34,"value":1080},{"type":25,"tag":88,"props":1300,"children":1301},{"style":101},[1302],{"type":34,"value":1085},{"type":25,"tag":88,"props":1304,"children":1305},{"style":95},[1306],{"type":34,"value":1307}," inspect\n",{"type":25,"tag":88,"props":1309,"children":1310},{"class":90,"line":118},[1311],{"type":25,"tag":88,"props":1312,"children":1313},{"emptyLinePlaceholder":122},[1314],{"type":34,"value":125},{"type":25,"tag":88,"props":1316,"children":1317},{"class":90,"line":128},[1318,1323,1328],{"type":25,"tag":88,"props":1319,"children":1320},{"style":101},[1321],{"type":34,"value":1322},"def",{"type":25,"tag":88,"props":1324,"children":1325},{"style":179},[1326],{"type":34,"value":1327}," get_database_schema",{"type":25,"tag":88,"props":1329,"children":1330},{"style":95},[1331],{"type":34,"value":1332},"(engine):\n",{"type":25,"tag":88,"props":1334,"children":1335},{"class":90,"line":152},[1336,1341,1345],{"type":25,"tag":88,"props":1337,"children":1338},{"style":95},[1339],{"type":34,"value":1340},"    inspector ",{"type":25,"tag":88,"props":1342,"children":1343},{"style":101},[1344],{"type":34,"value":104},{"type":25,"tag":88,"props":1346,"children":1347},{"style":95},[1348],{"type":34,"value":1349}," inspect(engine)\n",{"type":25,"tag":88,"props":1351,"children":1352},{"class":90,"line":170},[1353,1358,1362],{"type":25,"tag":88,"props":1354,"children":1355},{"style":95},[1356],{"type":34,"value":1357},"    schema_description ",{"type":25,"tag":88,"props":1359,"children":1360},{"style":101},[1361],{"type":34,"value":104},{"type":25,"tag":88,"props":1363,"children":1364},{"style":107},[1365],{"type":34,"value":1366}," \"\"\n",{"type":25,"tag":88,"props":1368,"children":1369},{"class":90,"line":200},[1370,1375,1380,1385],{"type":25,"tag":88,"props":1371,"children":1372},{"style":101},[1373],{"type":34,"value":1374},"    for",{"type":25,"tag":88,"props":1376,"children":1377},{"style":95},[1378],{"type":34,"value":1379}," table_name ",{"type":25,"tag":88,"props":1381,"children":1382},{"style":101},[1383],{"type":34,"value":1384},"in",{"type":25,"tag":88,"props":1386,"children":1387},{"style":95},[1388],{"type":34,"value":1389}," inspector.get_table_names():\n",{"type":25,"tag":88,"props":1391,"children":1392},{"class":90,"line":379},[1393,1398,1403,1408,1413,1418,1423,1428],{"type":25,"tag":88,"props":1394,"children":1395},{"style":95},[1396],{"type":34,"value":1397},"        schema_description ",{"type":25,"tag":88,"props":1399,"children":1400},{"style":101},[1401],{"type":34,"value":1402},"+=",{"type":25,"tag":88,"props":1404,"children":1405},{"style":101},[1406],{"type":34,"value":1407}," f",{"type":25,"tag":88,"props":1409,"children":1410},{"style":107},[1411],{"type":34,"value":1412},"\"Table: ",{"type":25,"tag":88,"props":1414,"children":1415},{"style":333},[1416],{"type":34,"value":1417},"{",{"type":25,"tag":88,"props":1419,"children":1420},{"style":95},[1421],{"type":34,"value":1422},"table_name",{"type":25,"tag":88,"props":1424,"children":1425},{"style":333},[1426],{"type":34,"value":1427},"}\\n",{"type":25,"tag":88,"props":1429,"children":1430},{"style":107},[1431],{"type":34,"value":1432},"\"\n",{"type":25,"tag":88,"props":1434,"children":1435},{"class":90,"line":561},[1436,1441,1446,1450],{"type":25,"tag":88,"props":1437,"children":1438},{"style":101},[1439],{"type":34,"value":1440},"        for",{"type":25,"tag":88,"props":1442,"children":1443},{"style":95},[1444],{"type":34,"value":1445}," column ",{"type":25,"tag":88,"props":1447,"children":1448},{"style":101},[1449],{"type":34,"value":1384},{"type":25,"tag":88,"props":1451,"children":1452},{"style":95},[1453],{"type":34,"value":1454}," inspector.get_columns(table_name):\n",{"type":25,"tag":88,"props":1456,"children":1457},{"class":90,"line":594},[1458,1463,1467,1471,1476,1480,1485,1490,1495,1500,1505,1509,1513,1518,1522,1526,1531,1536],{"type":25,"tag":88,"props":1459,"children":1460},{"style":95},[1461],{"type":34,"value":1462},"            schema_description ",{"type":25,"tag":88,"props":1464,"children":1465},{"style":101},[1466],{"type":34,"value":1402},{"type":25,"tag":88,"props":1468,"children":1469},{"style":101},[1470],{"type":34,"value":1407},{"type":25,"tag":88,"props":1472,"children":1473},{"style":107},[1474],{"type":34,"value":1475},"\"- ",{"type":25,"tag":88,"props":1477,"children":1478},{"style":333},[1479],{"type":34,"value":1417},{"type":25,"tag":88,"props":1481,"children":1482},{"style":95},[1483],{"type":34,"value":1484},"column[",{"type":25,"tag":88,"props":1486,"children":1487},{"style":107},[1488],{"type":34,"value":1489},"'name'",{"type":25,"tag":88,"props":1491,"children":1492},{"style":95},[1493],{"type":34,"value":1494},"]",{"type":25,"tag":88,"props":1496,"children":1497},{"style":333},[1498],{"type":34,"value":1499},"}",{"type":25,"tag":88,"props":1501,"children":1502},{"style":107},[1503],{"type":34,"value":1504}," (",{"type":25,"tag":88,"props":1506,"children":1507},{"style":333},[1508],{"type":34,"value":1417},{"type":25,"tag":88,"props":1510,"children":1511},{"style":95},[1512],{"type":34,"value":1484},{"type":25,"tag":88,"props":1514,"children":1515},{"style":107},[1516],{"type":34,"value":1517},"'type'",{"type":25,"tag":88,"props":1519,"children":1520},{"style":95},[1521],{"type":34,"value":1494},{"type":25,"tag":88,"props":1523,"children":1524},{"style":333},[1525],{"type":34,"value":1499},{"type":25,"tag":88,"props":1527,"children":1528},{"style":107},[1529],{"type":34,"value":1530},")",{"type":25,"tag":88,"props":1532,"children":1533},{"style":333},[1534],{"type":34,"value":1535},"\\n",{"type":25,"tag":88,"props":1537,"children":1538},{"style":107},[1539],{"type":34,"value":1432},{"type":25,"tag":88,"props":1541,"children":1542},{"class":90,"line":607},[1543,1547,1551,1556,1560],{"type":25,"tag":88,"props":1544,"children":1545},{"style":95},[1546],{"type":34,"value":1397},{"type":25,"tag":88,"props":1548,"children":1549},{"style":101},[1550],{"type":34,"value":1402},{"type":25,"tag":88,"props":1552,"children":1553},{"style":107},[1554],{"type":34,"value":1555}," \"",{"type":25,"tag":88,"props":1557,"children":1558},{"style":333},[1559],{"type":34,"value":1535},{"type":25,"tag":88,"props":1561,"children":1562},{"style":107},[1563],{"type":34,"value":1432},{"type":25,"tag":88,"props":1565,"children":1566},{"class":90,"line":1254},[1567,1572],{"type":25,"tag":88,"props":1568,"children":1569},{"style":101},[1570],{"type":34,"value":1571},"    return",{"type":25,"tag":88,"props":1573,"children":1574},{"style":95},[1575],{"type":34,"value":1576}," schema_description\n",{"type":25,"tag":88,"props":1578,"children":1580},{"class":90,"line":1579},12,[1581],{"type":25,"tag":88,"props":1582,"children":1583},{"emptyLinePlaceholder":122},[1584],{"type":34,"value":125},{"type":25,"tag":88,"props":1586,"children":1588},{"class":90,"line":1587},13,[1589,1594,1598],{"type":25,"tag":88,"props":1590,"children":1591},{"style":95},[1592],{"type":34,"value":1593},"schema_description ",{"type":25,"tag":88,"props":1595,"children":1596},{"style":101},[1597],{"type":34,"value":104},{"type":25,"tag":88,"props":1599,"children":1600},{"style":95},[1601],{"type":34,"value":1602}," get_database_schema(engine)\n",{"type":25,"tag":88,"props":1604,"children":1606},{"class":90,"line":1605},14,[1607,1612],{"type":25,"tag":88,"props":1608,"children":1609},{"style":333},[1610],{"type":34,"value":1611},"print",{"type":25,"tag":88,"props":1613,"children":1614},{"style":95},[1615],{"type":34,"value":1616},"(schema_description)\n",{"type":25,"tag":52,"props":1618,"children":1620},{"id":1619},"paso-4-preparar-la-consulta-con-gpt",[1621],{"type":34,"value":1622},"Paso 4: Preparar la consulta con GPT",{"type":25,"tag":30,"props":1624,"children":1625},{},[1626],{"type":34,"value":1627},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":25,"tag":79,"props":1629,"children":1631},{"className":1061,"code":1630,"language":1063,"meta":7,"style":7},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[1632],{"type":25,"tag":14,"props":1633,"children":1634},{"__ignoreMap":7},[1635,1647,1658,1665,1690,1697,1714,1731,1753,1770,1840,1873,1881,1889,1916,1929,1937,1965],{"type":25,"tag":88,"props":1636,"children":1637},{"class":90,"line":91},[1638,1642],{"type":25,"tag":88,"props":1639,"children":1640},{"style":101},[1641],{"type":34,"value":1085},{"type":25,"tag":88,"props":1643,"children":1644},{"style":95},[1645],{"type":34,"value":1646}," openai\n",{"type":25,"tag":88,"props":1648,"children":1649},{"class":90,"line":118},[1650,1654],{"type":25,"tag":88,"props":1651,"children":1652},{"style":101},[1653],{"type":34,"value":1085},{"type":25,"tag":88,"props":1655,"children":1656},{"style":95},[1657],{"type":34,"value":1123},{"type":25,"tag":88,"props":1659,"children":1660},{"class":90,"line":128},[1661],{"type":25,"tag":88,"props":1662,"children":1663},{"emptyLinePlaceholder":122},[1664],{"type":34,"value":125},{"type":25,"tag":88,"props":1666,"children":1667},{"class":90,"line":152},[1668,1673,1677,1681,1686],{"type":25,"tag":88,"props":1669,"children":1670},{"style":95},[1671],{"type":34,"value":1672},"openai.api_key ",{"type":25,"tag":88,"props":1674,"children":1675},{"style":101},[1676],{"type":34,"value":104},{"type":25,"tag":88,"props":1678,"children":1679},{"style":95},[1680],{"type":34,"value":1184},{"type":25,"tag":88,"props":1682,"children":1683},{"style":107},[1684],{"type":34,"value":1685},"\"OPENAI_API_KEY\"",{"type":25,"tag":88,"props":1687,"children":1688},{"style":95},[1689],{"type":34,"value":1194},{"type":25,"tag":88,"props":1691,"children":1692},{"class":90,"line":170},[1693],{"type":25,"tag":88,"props":1694,"children":1695},{"emptyLinePlaceholder":122},[1696],{"type":34,"value":125},{"type":25,"tag":88,"props":1698,"children":1699},{"class":90,"line":200},[1700,1704,1709],{"type":25,"tag":88,"props":1701,"children":1702},{"style":101},[1703],{"type":34,"value":1322},{"type":25,"tag":88,"props":1705,"children":1706},{"style":179},[1707],{"type":34,"value":1708}," generate_sql",{"type":25,"tag":88,"props":1710,"children":1711},{"style":95},[1712],{"type":34,"value":1713},"(question, schema):\n",{"type":25,"tag":88,"props":1715,"children":1716},{"class":90,"line":379},[1717,1722,1726],{"type":25,"tag":88,"props":1718,"children":1719},{"style":95},[1720],{"type":34,"value":1721},"    response ",{"type":25,"tag":88,"props":1723,"children":1724},{"style":101},[1725],{"type":34,"value":104},{"type":25,"tag":88,"props":1727,"children":1728},{"style":95},[1729],{"type":34,"value":1730}," openai.ChatCompletion.create(\n",{"type":25,"tag":88,"props":1732,"children":1733},{"class":90,"line":561},[1734,1739,1743,1748],{"type":25,"tag":88,"props":1735,"children":1736},{"style":1239},[1737],{"type":34,"value":1738},"        model",{"type":25,"tag":88,"props":1740,"children":1741},{"style":101},[1742],{"type":34,"value":104},{"type":25,"tag":88,"props":1744,"children":1745},{"style":107},[1746],{"type":34,"value":1747},"\"gpt-4\"",{"type":25,"tag":88,"props":1749,"children":1750},{"style":95},[1751],{"type":34,"value":1752},",\n",{"type":25,"tag":88,"props":1754,"children":1755},{"class":90,"line":594},[1756,1761,1765],{"type":25,"tag":88,"props":1757,"children":1758},{"style":1239},[1759],{"type":34,"value":1760},"        messages",{"type":25,"tag":88,"props":1762,"children":1763},{"style":101},[1764],{"type":34,"value":104},{"type":25,"tag":88,"props":1766,"children":1767},{"style":95},[1768],{"type":34,"value":1769},"[\n",{"type":25,"tag":88,"props":1771,"children":1772},{"class":90,"line":607},[1773,1778,1783,1788,1793,1797,1802,1806,1811,1816,1821,1826,1830,1835],{"type":25,"tag":88,"props":1774,"children":1775},{"style":95},[1776],{"type":34,"value":1777},"            {",{"type":25,"tag":88,"props":1779,"children":1780},{"style":107},[1781],{"type":34,"value":1782},"\"role\"",{"type":25,"tag":88,"props":1784,"children":1785},{"style":95},[1786],{"type":34,"value":1787},": ",{"type":25,"tag":88,"props":1789,"children":1790},{"style":107},[1791],{"type":34,"value":1792},"\"system\"",{"type":25,"tag":88,"props":1794,"children":1795},{"style":95},[1796],{"type":34,"value":479},{"type":25,"tag":88,"props":1798,"children":1799},{"style":107},[1800],{"type":34,"value":1801},"\"content\"",{"type":25,"tag":88,"props":1803,"children":1804},{"style":95},[1805],{"type":34,"value":1787},{"type":25,"tag":88,"props":1807,"children":1808},{"style":101},[1809],{"type":34,"value":1810},"f",{"type":25,"tag":88,"props":1812,"children":1813},{"style":107},[1814],{"type":34,"value":1815},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":25,"tag":88,"props":1817,"children":1818},{"style":333},[1819],{"type":34,"value":1820},"\\n{",{"type":25,"tag":88,"props":1822,"children":1823},{"style":95},[1824],{"type":34,"value":1825},"schema",{"type":25,"tag":88,"props":1827,"children":1828},{"style":333},[1829],{"type":34,"value":1499},{"type":25,"tag":88,"props":1831,"children":1832},{"style":107},[1833],{"type":34,"value":1834},"\"",{"type":25,"tag":88,"props":1836,"children":1837},{"style":95},[1838],{"type":34,"value":1839},"},\n",{"type":25,"tag":88,"props":1841,"children":1842},{"class":90,"line":1254},[1843,1847,1851,1855,1860,1864,1868],{"type":25,"tag":88,"props":1844,"children":1845},{"style":95},[1846],{"type":34,"value":1777},{"type":25,"tag":88,"props":1848,"children":1849},{"style":107},[1850],{"type":34,"value":1782},{"type":25,"tag":88,"props":1852,"children":1853},{"style":95},[1854],{"type":34,"value":1787},{"type":25,"tag":88,"props":1856,"children":1857},{"style":107},[1858],{"type":34,"value":1859},"\"user\"",{"type":25,"tag":88,"props":1861,"children":1862},{"style":95},[1863],{"type":34,"value":479},{"type":25,"tag":88,"props":1865,"children":1866},{"style":107},[1867],{"type":34,"value":1801},{"type":25,"tag":88,"props":1869,"children":1870},{"style":95},[1871],{"type":34,"value":1872},": question}\n",{"type":25,"tag":88,"props":1874,"children":1875},{"class":90,"line":1579},[1876],{"type":25,"tag":88,"props":1877,"children":1878},{"style":95},[1879],{"type":34,"value":1880},"        ]\n",{"type":25,"tag":88,"props":1882,"children":1883},{"class":90,"line":1587},[1884],{"type":25,"tag":88,"props":1885,"children":1886},{"style":95},[1887],{"type":34,"value":1888},"    )\n",{"type":25,"tag":88,"props":1890,"children":1891},{"class":90,"line":1605},[1892,1897,1901,1906,1911],{"type":25,"tag":88,"props":1893,"children":1894},{"style":95},[1895],{"type":34,"value":1896},"    sql_query ",{"type":25,"tag":88,"props":1898,"children":1899},{"style":101},[1900],{"type":34,"value":104},{"type":25,"tag":88,"props":1902,"children":1903},{"style":95},[1904],{"type":34,"value":1905}," response.choices[",{"type":25,"tag":88,"props":1907,"children":1908},{"style":333},[1909],{"type":34,"value":1910},"0",{"type":25,"tag":88,"props":1912,"children":1913},{"style":95},[1914],{"type":34,"value":1915},"].message.content.strip()\n",{"type":25,"tag":88,"props":1917,"children":1919},{"class":90,"line":1918},15,[1920,1924],{"type":25,"tag":88,"props":1921,"children":1922},{"style":101},[1923],{"type":34,"value":1571},{"type":25,"tag":88,"props":1925,"children":1926},{"style":95},[1927],{"type":34,"value":1928}," sql_query\n",{"type":25,"tag":88,"props":1930,"children":1932},{"class":90,"line":1931},16,[1933],{"type":25,"tag":88,"props":1934,"children":1935},{"emptyLinePlaceholder":122},[1936],{"type":34,"value":125},{"type":25,"tag":88,"props":1938,"children":1940},{"class":90,"line":1939},17,[1941,1946,1950,1955,1960],{"type":25,"tag":88,"props":1942,"children":1943},{"style":95},[1944],{"type":34,"value":1945},"query ",{"type":25,"tag":88,"props":1947,"children":1948},{"style":101},[1949],{"type":34,"value":104},{"type":25,"tag":88,"props":1951,"children":1952},{"style":95},[1953],{"type":34,"value":1954}," generate_sql(",{"type":25,"tag":88,"props":1956,"children":1957},{"style":107},[1958],{"type":34,"value":1959},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":25,"tag":88,"props":1961,"children":1962},{"style":95},[1963],{"type":34,"value":1964},", schema_description)\n",{"type":25,"tag":88,"props":1966,"children":1968},{"class":90,"line":1967},18,[1969,1973],{"type":25,"tag":88,"props":1970,"children":1971},{"style":333},[1972],{"type":34,"value":1611},{"type":25,"tag":88,"props":1974,"children":1975},{"style":95},[1976],{"type":34,"value":1977},"(query)\n",{"type":25,"tag":52,"props":1979,"children":1981},{"id":1980},"paso-5-ejecutar-consultas-generadas",[1982],{"type":34,"value":1983},"Paso 5: Ejecutar consultas generadas",{"type":25,"tag":30,"props":1985,"children":1986},{},[1987],{"type":34,"value":1988},"Ejecuta la consulta generada y obtiene resultados:",{"type":25,"tag":79,"props":1990,"children":1992},{"className":1061,"code":1991,"language":1063,"meta":7,"style":7},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[1993],{"type":25,"tag":14,"props":1994,"children":1995},{"__ignoreMap":7},[1996,2013,2035],{"type":25,"tag":88,"props":1997,"children":1998},{"class":90,"line":91},[1999,2004,2008],{"type":25,"tag":88,"props":2000,"children":2001},{"style":95},[2002],{"type":34,"value":2003},"result ",{"type":25,"tag":88,"props":2005,"children":2006},{"style":101},[2007],{"type":34,"value":104},{"type":25,"tag":88,"props":2009,"children":2010},{"style":95},[2011],{"type":34,"value":2012}," session.execute(query)\n",{"type":25,"tag":88,"props":2014,"children":2015},{"class":90,"line":118},[2016,2021,2026,2030],{"type":25,"tag":88,"props":2017,"children":2018},{"style":101},[2019],{"type":34,"value":2020},"for",{"type":25,"tag":88,"props":2022,"children":2023},{"style":95},[2024],{"type":34,"value":2025}," row ",{"type":25,"tag":88,"props":2027,"children":2028},{"style":101},[2029],{"type":34,"value":1384},{"type":25,"tag":88,"props":2031,"children":2032},{"style":95},[2033],{"type":34,"value":2034}," result:\n",{"type":25,"tag":88,"props":2036,"children":2037},{"class":90,"line":128},[2038,2043],{"type":25,"tag":88,"props":2039,"children":2040},{"style":333},[2041],{"type":34,"value":2042},"    print",{"type":25,"tag":88,"props":2044,"children":2045},{"style":95},[2046],{"type":34,"value":2047},"(row)\n",{"type":25,"tag":52,"props":2049,"children":2051},{"id":2050},"paso-6-manejo-de-errores-y-seguridad",[2052],{"type":34,"value":2053},"Paso 6: Manejo de errores y seguridad",{"type":25,"tag":30,"props":2055,"children":2056},{},[2057],{"type":34,"value":2058},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":25,"tag":79,"props":2060,"children":2062},{"className":1061,"code":2061,"language":1063,"meta":7,"style":7},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[2063],{"type":25,"tag":14,"props":2064,"children":2065},{"__ignoreMap":7},[2066,2083,2096,2113,2126,2149,2191,2203,2210,2227],{"type":25,"tag":88,"props":2067,"children":2068},{"class":90,"line":91},[2069,2073,2078],{"type":25,"tag":88,"props":2070,"children":2071},{"style":101},[2072],{"type":34,"value":1322},{"type":25,"tag":88,"props":2074,"children":2075},{"style":179},[2076],{"type":34,"value":2077}," execute_safe_query",{"type":25,"tag":88,"props":2079,"children":2080},{"style":95},[2081],{"type":34,"value":2082},"(sql_query):\n",{"type":25,"tag":88,"props":2084,"children":2085},{"class":90,"line":118},[2086,2091],{"type":25,"tag":88,"props":2087,"children":2088},{"style":101},[2089],{"type":34,"value":2090},"    try",{"type":25,"tag":88,"props":2092,"children":2093},{"style":95},[2094],{"type":34,"value":2095},":\n",{"type":25,"tag":88,"props":2097,"children":2098},{"class":90,"line":128},[2099,2104,2108],{"type":25,"tag":88,"props":2100,"children":2101},{"style":95},[2102],{"type":34,"value":2103},"        result ",{"type":25,"tag":88,"props":2105,"children":2106},{"style":101},[2107],{"type":34,"value":104},{"type":25,"tag":88,"props":2109,"children":2110},{"style":95},[2111],{"type":34,"value":2112}," session.execute(sql_query)\n",{"type":25,"tag":88,"props":2114,"children":2115},{"class":90,"line":152},[2116,2121],{"type":25,"tag":88,"props":2117,"children":2118},{"style":101},[2119],{"type":34,"value":2120},"        return",{"type":25,"tag":88,"props":2122,"children":2123},{"style":95},[2124],{"type":34,"value":2125}," result.fetchall()\n",{"type":25,"tag":88,"props":2127,"children":2128},{"class":90,"line":170},[2129,2134,2139,2144],{"type":25,"tag":88,"props":2130,"children":2131},{"style":101},[2132],{"type":34,"value":2133},"    except",{"type":25,"tag":88,"props":2135,"children":2136},{"style":333},[2137],{"type":34,"value":2138}," Exception",{"type":25,"tag":88,"props":2140,"children":2141},{"style":101},[2142],{"type":34,"value":2143}," as",{"type":25,"tag":88,"props":2145,"children":2146},{"style":95},[2147],{"type":34,"value":2148}," e:\n",{"type":25,"tag":88,"props":2150,"children":2151},{"class":90,"line":200},[2152,2157,2161,2165,2170,2174,2179,2183,2187],{"type":25,"tag":88,"props":2153,"children":2154},{"style":333},[2155],{"type":34,"value":2156},"        print",{"type":25,"tag":88,"props":2158,"children":2159},{"style":95},[2160],{"type":34,"value":367},{"type":25,"tag":88,"props":2162,"children":2163},{"style":101},[2164],{"type":34,"value":1810},{"type":25,"tag":88,"props":2166,"children":2167},{"style":107},[2168],{"type":34,"value":2169},"\"Error al ejecutar consulta: ",{"type":25,"tag":88,"props":2171,"children":2172},{"style":333},[2173],{"type":34,"value":1417},{"type":25,"tag":88,"props":2175,"children":2176},{"style":95},[2177],{"type":34,"value":2178},"e",{"type":25,"tag":88,"props":2180,"children":2181},{"style":333},[2182],{"type":34,"value":1499},{"type":25,"tag":88,"props":2184,"children":2185},{"style":107},[2186],{"type":34,"value":1834},{"type":25,"tag":88,"props":2188,"children":2189},{"style":95},[2190],{"type":34,"value":1194},{"type":25,"tag":88,"props":2192,"children":2193},{"class":90,"line":379},[2194,2198],{"type":25,"tag":88,"props":2195,"children":2196},{"style":101},[2197],{"type":34,"value":2120},{"type":25,"tag":88,"props":2199,"children":2200},{"style":333},[2201],{"type":34,"value":2202}," None\n",{"type":25,"tag":88,"props":2204,"children":2205},{"class":90,"line":561},[2206],{"type":25,"tag":88,"props":2207,"children":2208},{"emptyLinePlaceholder":122},[2209],{"type":34,"value":125},{"type":25,"tag":88,"props":2211,"children":2212},{"class":90,"line":594},[2213,2218,2222],{"type":25,"tag":88,"props":2214,"children":2215},{"style":95},[2216],{"type":34,"value":2217},"results ",{"type":25,"tag":88,"props":2219,"children":2220},{"style":101},[2221],{"type":34,"value":104},{"type":25,"tag":88,"props":2223,"children":2224},{"style":95},[2225],{"type":34,"value":2226}," execute_safe_query(query)\n",{"type":25,"tag":88,"props":2228,"children":2229},{"class":90,"line":607},[2230,2234],{"type":25,"tag":88,"props":2231,"children":2232},{"style":333},[2233],{"type":34,"value":1611},{"type":25,"tag":88,"props":2235,"children":2236},{"style":95},[2237],{"type":34,"value":2238},"(results)\n",{"type":25,"tag":52,"props":2240,"children":2242},{"id":2241},"buenas-prácticas",[2243],{"type":34,"value":2244},"Buenas prácticas",{"type":25,"tag":392,"props":2246,"children":2247},{},[2248,2253,2258],{"type":25,"tag":396,"props":2249,"children":2250},{},[2251],{"type":34,"value":2252},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":25,"tag":396,"props":2254,"children":2255},{},[2256],{"type":34,"value":2257},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":25,"tag":396,"props":2259,"children":2260},{},[2261],{"type":34,"value":2262},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":25,"tag":52,"props":2264,"children":2266},{"id":2265},"conclusión",[2267],{"type":34,"value":2268},"Conclusión",{"type":25,"tag":30,"props":2270,"children":2271},{},[2272],{"type":34,"value":2273},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":25,"tag":861,"props":2275,"children":2277},{"id":2276},"recursos-adicionales",[2278],{"type":34,"value":2279},"Recursos adicionales",{"type":25,"tag":392,"props":2281,"children":2282},{},[2283,2293,2303],{"type":25,"tag":396,"props":2284,"children":2285},{},[2286],{"type":25,"tag":874,"props":2287,"children":2290},{"href":2288,"rel":2289},"https://platform.openai.com/docs/api-reference",[878],[2291],{"type":34,"value":2292},"Documentación API OpenAI",{"type":25,"tag":396,"props":2294,"children":2295},{},[2296],{"type":25,"tag":874,"props":2297,"children":2300},{"href":2298,"rel":2299},"https://docs.sqlalchemy.org/en/20/",[878],[2301],{"type":34,"value":2302},"SQLAlchemy ORM Documentation",{"type":25,"tag":396,"props":2304,"children":2305},{},[2306],{"type":25,"tag":874,"props":2307,"children":2310},{"href":2308,"rel":2309},"https://pypi.org/project/python-dotenv/",[878],[2311],{"type":34,"value":2312},"Python Dotenv",{"type":25,"tag":893,"props":2314,"children":2315},{},[2316],{"type":34,"value":897},{"title":7,"searchDepth":118,"depth":118,"links":2318},[2319,2320,2321,2322,2323,2324,2325,2326,2327],{"id":944,"depth":118,"text":947},{"id":968,"depth":118,"text":971},{"id":1050,"depth":118,"text":1053},{"id":1272,"depth":118,"text":1275},{"id":1619,"depth":118,"text":1622},{"id":1980,"depth":118,"text":1983},{"id":2050,"depth":118,"text":2053},{"id":2241,"depth":118,"text":2244},{"id":2265,"depth":118,"text":2268},"content:posts:gpt-sql-integration.md","posts/gpt-sql-integration.md","posts/gpt-sql-integration",{"_path":2332,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":2333,"description":2334,"date":2335,"layout":11,"subtitle":2336,"image":2337,"optimized_image":2337,"category":14,"tags":2338,"author":20,"paginate":6,"body":2342,"_type":912,"_id":3226,"_source":914,"_file":3227,"_stem":3228,"_extension":917},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","Explore essential security strategies for Java applications, addressing common vulnerabilities identified by OWASP Top 10 with practical examples.","2024-08-15T11:00:00.000Z","Practical Strategies to Safeguard Your Java Applications","/assets/img/uploads/java-security-best-practices.png",[14,16,2339,2340,2341],"security","owasp","cybersecurity",{"type":22,"children":2343,"toc":3214},[2344,2352,2358,2363,2417,2422,2428,2433,2553,2558,2564,2569,2669,2675,2680,2803,2808,2814,2819,2844,2849,2855,2860,2876,2881,2887,2892,2974,2979,2985,2990,3110,3115,3121,3126,3159,3164,3168,3173,3177,3210],{"type":25,"tag":26,"props":2345,"children":2346},{},[2347],{"type":25,"tag":30,"props":2348,"children":2349},{},[2350],{"type":34,"value":2351},"Security is critical in software development. Java applications, like all software, face numerous potential vulnerabilities. In this article, we will explore essential security best practices, referencing the OWASP Top 10, and providing practical Java examples to help you protect your applications effectively.",{"type":25,"tag":52,"props":2353,"children":2355},{"id":2354},"understanding-owasp-top-10",[2356],{"type":34,"value":2357},"Understanding OWASP Top 10",{"type":25,"tag":30,"props":2359,"children":2360},{},[2361],{"type":34,"value":2362},"The OWASP Top 10 outlines the most critical web application security risks:",{"type":25,"tag":2364,"props":2365,"children":2366},"ol",{},[2367,2372,2377,2382,2387,2392,2397,2402,2407,2412],{"type":25,"tag":396,"props":2368,"children":2369},{},[2370],{"type":34,"value":2371},"Injection",{"type":25,"tag":396,"props":2373,"children":2374},{},[2375],{"type":34,"value":2376},"Broken Authentication",{"type":25,"tag":396,"props":2378,"children":2379},{},[2380],{"type":34,"value":2381},"Sensitive Data Exposure",{"type":25,"tag":396,"props":2383,"children":2384},{},[2385],{"type":34,"value":2386},"XML External Entities (XXE)",{"type":25,"tag":396,"props":2388,"children":2389},{},[2390],{"type":34,"value":2391},"Broken Access Control",{"type":25,"tag":396,"props":2393,"children":2394},{},[2395],{"type":34,"value":2396},"Security Misconfiguration",{"type":25,"tag":396,"props":2398,"children":2399},{},[2400],{"type":34,"value":2401},"Cross-Site Scripting (XSS)",{"type":25,"tag":396,"props":2403,"children":2404},{},[2405],{"type":34,"value":2406},"Insecure Deserialization",{"type":25,"tag":396,"props":2408,"children":2409},{},[2410],{"type":34,"value":2411},"Using Components with Known Vulnerabilities",{"type":25,"tag":396,"props":2413,"children":2414},{},[2415],{"type":34,"value":2416},"Insufficient Logging and Monitoring",{"type":25,"tag":30,"props":2418,"children":2419},{},[2420],{"type":34,"value":2421},"Let's delve into practical strategies and Java implementations for some of these threats.",{"type":25,"tag":52,"props":2423,"children":2425},{"id":2424},"_1-injection-prevention",[2426],{"type":34,"value":2427},"1. Injection Prevention",{"type":25,"tag":30,"props":2429,"children":2430},{},[2431],{"type":34,"value":2432},"Use Prepared Statements and parameterized queries:",{"type":25,"tag":79,"props":2434,"children":2436},{"className":81,"code":2435,"language":16,"meta":7,"style":7},"PreparedStatement stmt = connection.prepareStatement(\"SELECT * FROM users WHERE username=? AND password=?\");\nstmt.setString(1, username);\nstmt.setString(2, password);\nResultSet rs = stmt.executeQuery();\n",[2437],{"type":25,"tag":14,"props":2438,"children":2439},{"__ignoreMap":7},[2440,2475,2502,2527],{"type":25,"tag":88,"props":2441,"children":2442},{"class":90,"line":91},[2443,2448,2452,2457,2462,2466,2471],{"type":25,"tag":88,"props":2444,"children":2445},{"style":95},[2446],{"type":34,"value":2447},"PreparedStatement stmt ",{"type":25,"tag":88,"props":2449,"children":2450},{"style":101},[2451],{"type":34,"value":104},{"type":25,"tag":88,"props":2453,"children":2454},{"style":95},[2455],{"type":34,"value":2456}," connection.",{"type":25,"tag":88,"props":2458,"children":2459},{"style":179},[2460],{"type":34,"value":2461},"prepareStatement",{"type":25,"tag":88,"props":2463,"children":2464},{"style":95},[2465],{"type":34,"value":367},{"type":25,"tag":88,"props":2467,"children":2468},{"style":107},[2469],{"type":34,"value":2470},"\"SELECT * FROM users WHERE username=? AND password=?\"",{"type":25,"tag":88,"props":2472,"children":2473},{"style":95},[2474],{"type":34,"value":341},{"type":25,"tag":88,"props":2476,"children":2477},{"class":90,"line":118},[2478,2483,2488,2492,2497],{"type":25,"tag":88,"props":2479,"children":2480},{"style":95},[2481],{"type":34,"value":2482},"stmt.",{"type":25,"tag":88,"props":2484,"children":2485},{"style":179},[2486],{"type":34,"value":2487},"setString",{"type":25,"tag":88,"props":2489,"children":2490},{"style":95},[2491],{"type":34,"value":367},{"type":25,"tag":88,"props":2493,"children":2494},{"style":333},[2495],{"type":34,"value":2496},"1",{"type":25,"tag":88,"props":2498,"children":2499},{"style":95},[2500],{"type":34,"value":2501},", username);\n",{"type":25,"tag":88,"props":2503,"children":2504},{"class":90,"line":128},[2505,2509,2513,2517,2522],{"type":25,"tag":88,"props":2506,"children":2507},{"style":95},[2508],{"type":34,"value":2482},{"type":25,"tag":88,"props":2510,"children":2511},{"style":179},[2512],{"type":34,"value":2487},{"type":25,"tag":88,"props":2514,"children":2515},{"style":95},[2516],{"type":34,"value":367},{"type":25,"tag":88,"props":2518,"children":2519},{"style":333},[2520],{"type":34,"value":2521},"2",{"type":25,"tag":88,"props":2523,"children":2524},{"style":95},[2525],{"type":34,"value":2526},", password);\n",{"type":25,"tag":88,"props":2528,"children":2529},{"class":90,"line":152},[2530,2535,2539,2544,2549],{"type":25,"tag":88,"props":2531,"children":2532},{"style":95},[2533],{"type":34,"value":2534},"ResultSet rs ",{"type":25,"tag":88,"props":2536,"children":2537},{"style":101},[2538],{"type":34,"value":104},{"type":25,"tag":88,"props":2540,"children":2541},{"style":95},[2542],{"type":34,"value":2543}," stmt.",{"type":25,"tag":88,"props":2545,"children":2546},{"style":179},[2547],{"type":34,"value":2548},"executeQuery",{"type":25,"tag":88,"props":2550,"children":2551},{"style":95},[2552],{"type":34,"value":651},{"type":25,"tag":30,"props":2554,"children":2555},{},[2556],{"type":34,"value":2557},"This approach prevents SQL injection by ensuring that input data is treated as data, not executable code.",{"type":25,"tag":52,"props":2559,"children":2561},{"id":2560},"_2-secure-authentication",[2562],{"type":34,"value":2563},"2. Secure Authentication",{"type":25,"tag":30,"props":2565,"children":2566},{},[2567],{"type":34,"value":2568},"Implement robust authentication using secure password storage (bcrypt or Argon2):",{"type":25,"tag":79,"props":2570,"children":2572},{"className":81,"code":2571,"language":16,"meta":7,"style":7},"import org.springframework.security.crypto.bcrypt.BCrypt;\n\nString hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());\nif (BCrypt.checkpw(password, hashedPassword)) {\n    // authentication successful\n}\n",[2573],{"type":25,"tag":14,"props":2574,"children":2575},{"__ignoreMap":7},[2576,2588,2595,2631,2653,2662],{"type":25,"tag":88,"props":2577,"children":2578},{"class":90,"line":91},[2579,2583],{"type":25,"tag":88,"props":2580,"children":2581},{"style":101},[2582],{"type":34,"value":1085},{"type":25,"tag":88,"props":2584,"children":2585},{"style":95},[2586],{"type":34,"value":2587}," org.springframework.security.crypto.bcrypt.BCrypt;\n",{"type":25,"tag":88,"props":2589,"children":2590},{"class":90,"line":118},[2591],{"type":25,"tag":88,"props":2592,"children":2593},{"emptyLinePlaceholder":122},[2594],{"type":34,"value":125},{"type":25,"tag":88,"props":2596,"children":2597},{"class":90,"line":128},[2598,2603,2607,2612,2617,2622,2627],{"type":25,"tag":88,"props":2599,"children":2600},{"style":95},[2601],{"type":34,"value":2602},"String hashedPassword ",{"type":25,"tag":88,"props":2604,"children":2605},{"style":101},[2606],{"type":34,"value":104},{"type":25,"tag":88,"props":2608,"children":2609},{"style":95},[2610],{"type":34,"value":2611}," BCrypt.",{"type":25,"tag":88,"props":2613,"children":2614},{"style":179},[2615],{"type":34,"value":2616},"hashpw",{"type":25,"tag":88,"props":2618,"children":2619},{"style":95},[2620],{"type":34,"value":2621},"(password, BCrypt.",{"type":25,"tag":88,"props":2623,"children":2624},{"style":179},[2625],{"type":34,"value":2626},"gensalt",{"type":25,"tag":88,"props":2628,"children":2629},{"style":95},[2630],{"type":34,"value":197},{"type":25,"tag":88,"props":2632,"children":2633},{"class":90,"line":152},[2634,2638,2643,2648],{"type":25,"tag":88,"props":2635,"children":2636},{"style":101},[2637],{"type":34,"value":134},{"type":25,"tag":88,"props":2639,"children":2640},{"style":95},[2641],{"type":34,"value":2642}," (BCrypt.",{"type":25,"tag":88,"props":2644,"children":2645},{"style":179},[2646],{"type":34,"value":2647},"checkpw",{"type":25,"tag":88,"props":2649,"children":2650},{"style":95},[2651],{"type":34,"value":2652},"(password, hashedPassword)) {\n",{"type":25,"tag":88,"props":2654,"children":2655},{"class":90,"line":170},[2656],{"type":25,"tag":88,"props":2657,"children":2659},{"style":2658},"--shiki-default:#6A737D;--shiki-light:#6A737D",[2660],{"type":34,"value":2661},"    // authentication successful\n",{"type":25,"tag":88,"props":2663,"children":2664},{"class":90,"line":200},[2665],{"type":25,"tag":88,"props":2666,"children":2667},{"style":95},[2668],{"type":34,"value":206},{"type":25,"tag":52,"props":2670,"children":2672},{"id":2671},"_3-sensitive-data-exposure",[2673],{"type":34,"value":2674},"3. Sensitive Data Exposure",{"type":25,"tag":30,"props":2676,"children":2677},{},[2678],{"type":34,"value":2679},"Use encryption for sensitive data storage:",{"type":25,"tag":79,"props":2681,"children":2683},{"className":81,"code":2682,"language":16,"meta":7,"style":7},"Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nSecretKey key = generateSecretKey();\ncipher.init(Cipher.ENCRYPT_MODE, key);\nbyte[] encryptedData = cipher.doFinal(plainData.getBytes(StandardCharsets.UTF_8));\n",[2684],{"type":25,"tag":14,"props":2685,"children":2686},{"__ignoreMap":7},[2687,2722,2743,2761],{"type":25,"tag":88,"props":2688,"children":2689},{"class":90,"line":91},[2690,2695,2699,2704,2709,2713,2718],{"type":25,"tag":88,"props":2691,"children":2692},{"style":95},[2693],{"type":34,"value":2694},"Cipher cipher ",{"type":25,"tag":88,"props":2696,"children":2697},{"style":101},[2698],{"type":34,"value":104},{"type":25,"tag":88,"props":2700,"children":2701},{"style":95},[2702],{"type":34,"value":2703}," Cipher.",{"type":25,"tag":88,"props":2705,"children":2706},{"style":179},[2707],{"type":34,"value":2708},"getInstance",{"type":25,"tag":88,"props":2710,"children":2711},{"style":95},[2712],{"type":34,"value":367},{"type":25,"tag":88,"props":2714,"children":2715},{"style":107},[2716],{"type":34,"value":2717},"\"AES/GCM/NoPadding\"",{"type":25,"tag":88,"props":2719,"children":2720},{"style":95},[2721],{"type":34,"value":341},{"type":25,"tag":88,"props":2723,"children":2724},{"class":90,"line":118},[2725,2730,2734,2739],{"type":25,"tag":88,"props":2726,"children":2727},{"style":95},[2728],{"type":34,"value":2729},"SecretKey key ",{"type":25,"tag":88,"props":2731,"children":2732},{"style":101},[2733],{"type":34,"value":104},{"type":25,"tag":88,"props":2735,"children":2736},{"style":179},[2737],{"type":34,"value":2738}," generateSecretKey",{"type":25,"tag":88,"props":2740,"children":2741},{"style":95},[2742],{"type":34,"value":651},{"type":25,"tag":88,"props":2744,"children":2745},{"class":90,"line":128},[2746,2751,2756],{"type":25,"tag":88,"props":2747,"children":2748},{"style":95},[2749],{"type":34,"value":2750},"cipher.",{"type":25,"tag":88,"props":2752,"children":2753},{"style":179},[2754],{"type":34,"value":2755},"init",{"type":25,"tag":88,"props":2757,"children":2758},{"style":95},[2759],{"type":34,"value":2760},"(Cipher.ENCRYPT_MODE, key);\n",{"type":25,"tag":88,"props":2762,"children":2763},{"class":90,"line":152},[2764,2769,2774,2778,2783,2788,2793,2798],{"type":25,"tag":88,"props":2765,"children":2766},{"style":101},[2767],{"type":34,"value":2768},"byte",{"type":25,"tag":88,"props":2770,"children":2771},{"style":95},[2772],{"type":34,"value":2773},"[] encryptedData ",{"type":25,"tag":88,"props":2775,"children":2776},{"style":101},[2777],{"type":34,"value":104},{"type":25,"tag":88,"props":2779,"children":2780},{"style":95},[2781],{"type":34,"value":2782}," cipher.",{"type":25,"tag":88,"props":2784,"children":2785},{"style":179},[2786],{"type":34,"value":2787},"doFinal",{"type":25,"tag":88,"props":2789,"children":2790},{"style":95},[2791],{"type":34,"value":2792},"(plainData.",{"type":25,"tag":88,"props":2794,"children":2795},{"style":179},[2796],{"type":34,"value":2797},"getBytes",{"type":25,"tag":88,"props":2799,"children":2800},{"style":95},[2801],{"type":34,"value":2802},"(StandardCharsets.UTF_8));\n",{"type":25,"tag":30,"props":2804,"children":2805},{},[2806],{"type":34,"value":2807},"Always encrypt data at rest and in transit (use HTTPS).",{"type":25,"tag":52,"props":2809,"children":2811},{"id":2810},"_4-security-misconfiguration",[2812],{"type":34,"value":2813},"4. Security Misconfiguration",{"type":25,"tag":30,"props":2815,"children":2816},{},[2817],{"type":34,"value":2818},"Adopt secure defaults, disable debugging, and avoid detailed error messages:",{"type":25,"tag":79,"props":2820,"children":2824},{"className":2821,"code":2822,"language":2823,"meta":7,"style":7},"language-properties shiki shiki-themes github-dark github-light","server.error.include-stacktrace=never\nserver.error.include-message=never\n","properties",[2825],{"type":25,"tag":14,"props":2826,"children":2827},{"__ignoreMap":7},[2828,2836],{"type":25,"tag":88,"props":2829,"children":2830},{"class":90,"line":91},[2831],{"type":25,"tag":88,"props":2832,"children":2833},{},[2834],{"type":34,"value":2835},"server.error.include-stacktrace=never\n",{"type":25,"tag":88,"props":2837,"children":2838},{"class":90,"line":118},[2839],{"type":25,"tag":88,"props":2840,"children":2841},{},[2842],{"type":34,"value":2843},"server.error.include-message=never\n",{"type":25,"tag":30,"props":2845,"children":2846},{},[2847],{"type":34,"value":2848},"Review and secure your Java application configuration regularly.",{"type":25,"tag":52,"props":2850,"children":2852},{"id":2851},"_5-cross-site-scripting-xss",[2853],{"type":34,"value":2854},"5. Cross-Site Scripting (XSS)",{"type":25,"tag":30,"props":2856,"children":2857},{},[2858],{"type":34,"value":2859},"Utilize proper encoding or frameworks that prevent XSS by default, such as Thymeleaf:",{"type":25,"tag":79,"props":2861,"children":2865},{"className":2862,"code":2863,"language":2864,"meta":7,"style":7},"language-html shiki shiki-themes github-dark github-light","\u003Cp th:text=\"${userInput}\">\u003C/p>\n","html",[2866],{"type":25,"tag":14,"props":2867,"children":2868},{"__ignoreMap":7},[2869],{"type":25,"tag":88,"props":2870,"children":2871},{"class":90,"line":91},[2872],{"type":25,"tag":88,"props":2873,"children":2874},{},[2875],{"type":34,"value":2863},{"type":25,"tag":30,"props":2877,"children":2878},{},[2879],{"type":34,"value":2880},"Avoid directly inserting user inputs into HTML without proper sanitization or encoding.",{"type":25,"tag":52,"props":2882,"children":2884},{"id":2883},"_6-insecure-deserialization",[2885],{"type":34,"value":2886},"6. Insecure Deserialization",{"type":25,"tag":30,"props":2888,"children":2889},{},[2890],{"type":34,"value":2891},"Limit deserialization to trusted sources only:",{"type":25,"tag":79,"props":2893,"children":2895},{"className":81,"code":2894,"language":16,"meta":7,"style":7},"ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"trusted_data.ser\"));\nMyObject obj = (MyObject) ois.readObject();\n",[2896],{"type":25,"tag":14,"props":2897,"children":2898},{"__ignoreMap":7},[2899,2948],{"type":25,"tag":88,"props":2900,"children":2901},{"class":90,"line":91},[2902,2907,2911,2915,2920,2924,2929,2934,2938,2943],{"type":25,"tag":88,"props":2903,"children":2904},{"style":95},[2905],{"type":34,"value":2906},"ObjectInputStream ois ",{"type":25,"tag":88,"props":2908,"children":2909},{"style":101},[2910],{"type":34,"value":104},{"type":25,"tag":88,"props":2912,"children":2913},{"style":101},[2914],{"type":34,"value":642},{"type":25,"tag":88,"props":2916,"children":2917},{"style":179},[2918],{"type":34,"value":2919}," ObjectInputStream",{"type":25,"tag":88,"props":2921,"children":2922},{"style":95},[2923],{"type":34,"value":367},{"type":25,"tag":88,"props":2925,"children":2926},{"style":101},[2927],{"type":34,"value":2928},"new",{"type":25,"tag":88,"props":2930,"children":2931},{"style":179},[2932],{"type":34,"value":2933}," FileInputStream",{"type":25,"tag":88,"props":2935,"children":2936},{"style":95},[2937],{"type":34,"value":367},{"type":25,"tag":88,"props":2939,"children":2940},{"style":107},[2941],{"type":34,"value":2942},"\"trusted_data.ser\"",{"type":25,"tag":88,"props":2944,"children":2945},{"style":95},[2946],{"type":34,"value":2947},"));\n",{"type":25,"tag":88,"props":2949,"children":2950},{"class":90,"line":118},[2951,2956,2960,2965,2970],{"type":25,"tag":88,"props":2952,"children":2953},{"style":95},[2954],{"type":34,"value":2955},"MyObject obj ",{"type":25,"tag":88,"props":2957,"children":2958},{"style":101},[2959],{"type":34,"value":104},{"type":25,"tag":88,"props":2961,"children":2962},{"style":95},[2963],{"type":34,"value":2964}," (MyObject) ois.",{"type":25,"tag":88,"props":2966,"children":2967},{"style":179},[2968],{"type":34,"value":2969},"readObject",{"type":25,"tag":88,"props":2971,"children":2972},{"style":95},[2973],{"type":34,"value":651},{"type":25,"tag":30,"props":2975,"children":2976},{},[2977],{"type":34,"value":2978},"Always validate deserialized data.",{"type":25,"tag":52,"props":2980,"children":2982},{"id":2981},"_7-component-vulnerabilities",[2983],{"type":34,"value":2984},"7. Component Vulnerabilities",{"type":25,"tag":30,"props":2986,"children":2987},{},[2988],{"type":34,"value":2989},"Regularly update libraries and dependencies, and use tools like OWASP Dependency-Check to identify vulnerabilities:",{"type":25,"tag":79,"props":2991,"children":2995},{"className":2992,"code":2993,"language":2994,"meta":7,"style":7},"language-xml shiki shiki-themes github-dark github-light","\u003Cplugin>\n    \u003CgroupId>org.owasp\u003C/groupId>\n    \u003CartifactId>dependency-check-maven\u003C/artifactId>\n    \u003Cversion>9.0.9\u003C/version>\n\u003C/plugin>\n","xml",[2996],{"type":25,"tag":14,"props":2997,"children":2998},{"__ignoreMap":7},[2999,3018,3044,3069,3094],{"type":25,"tag":88,"props":3000,"children":3001},{"class":90,"line":91},[3002,3007,3013],{"type":25,"tag":88,"props":3003,"children":3004},{"style":95},[3005],{"type":34,"value":3006},"\u003C",{"type":25,"tag":88,"props":3008,"children":3010},{"style":3009},"--shiki-default:#85E89D;--shiki-light:#22863A",[3011],{"type":34,"value":3012},"plugin",{"type":25,"tag":88,"props":3014,"children":3015},{"style":95},[3016],{"type":34,"value":3017},">\n",{"type":25,"tag":88,"props":3019,"children":3020},{"class":90,"line":118},[3021,3026,3031,3036,3040],{"type":25,"tag":88,"props":3022,"children":3023},{"style":95},[3024],{"type":34,"value":3025},"    \u003C",{"type":25,"tag":88,"props":3027,"children":3028},{"style":3009},[3029],{"type":34,"value":3030},"groupId",{"type":25,"tag":88,"props":3032,"children":3033},{"style":95},[3034],{"type":34,"value":3035},">org.owasp\u003C/",{"type":25,"tag":88,"props":3037,"children":3038},{"style":3009},[3039],{"type":34,"value":3030},{"type":25,"tag":88,"props":3041,"children":3042},{"style":95},[3043],{"type":34,"value":3017},{"type":25,"tag":88,"props":3045,"children":3046},{"class":90,"line":128},[3047,3051,3056,3061,3065],{"type":25,"tag":88,"props":3048,"children":3049},{"style":95},[3050],{"type":34,"value":3025},{"type":25,"tag":88,"props":3052,"children":3053},{"style":3009},[3054],{"type":34,"value":3055},"artifactId",{"type":25,"tag":88,"props":3057,"children":3058},{"style":95},[3059],{"type":34,"value":3060},">dependency-check-maven\u003C/",{"type":25,"tag":88,"props":3062,"children":3063},{"style":3009},[3064],{"type":34,"value":3055},{"type":25,"tag":88,"props":3066,"children":3067},{"style":95},[3068],{"type":34,"value":3017},{"type":25,"tag":88,"props":3070,"children":3071},{"class":90,"line":152},[3072,3076,3081,3086,3090],{"type":25,"tag":88,"props":3073,"children":3074},{"style":95},[3075],{"type":34,"value":3025},{"type":25,"tag":88,"props":3077,"children":3078},{"style":3009},[3079],{"type":34,"value":3080},"version",{"type":25,"tag":88,"props":3082,"children":3083},{"style":95},[3084],{"type":34,"value":3085},">9.0.9\u003C/",{"type":25,"tag":88,"props":3087,"children":3088},{"style":3009},[3089],{"type":34,"value":3080},{"type":25,"tag":88,"props":3091,"children":3092},{"style":95},[3093],{"type":34,"value":3017},{"type":25,"tag":88,"props":3095,"children":3096},{"class":90,"line":170},[3097,3102,3106],{"type":25,"tag":88,"props":3098,"children":3099},{"style":95},[3100],{"type":34,"value":3101},"\u003C/",{"type":25,"tag":88,"props":3103,"children":3104},{"style":3009},[3105],{"type":34,"value":3012},{"type":25,"tag":88,"props":3107,"children":3108},{"style":95},[3109],{"type":34,"value":3017},{"type":25,"tag":30,"props":3111,"children":3112},{},[3113],{"type":34,"value":3114},"Execute dependency checks regularly in your CI/CD pipeline.",{"type":25,"tag":52,"props":3116,"children":3118},{"id":3117},"_8-logging-and-monitoring",[3119],{"type":34,"value":3120},"8. Logging and Monitoring",{"type":25,"tag":30,"props":3122,"children":3123},{},[3124],{"type":34,"value":3125},"Implement comprehensive logging and monitoring to detect and respond to security incidents:",{"type":25,"tag":79,"props":3127,"children":3129},{"className":81,"code":3128,"language":16,"meta":7,"style":7},"logger.warn(\"Failed login attempt for user: {}\", username);\n",[3130],{"type":25,"tag":14,"props":3131,"children":3132},{"__ignoreMap":7},[3133],{"type":25,"tag":88,"props":3134,"children":3135},{"class":90,"line":91},[3136,3141,3146,3150,3155],{"type":25,"tag":88,"props":3137,"children":3138},{"style":95},[3139],{"type":34,"value":3140},"logger.",{"type":25,"tag":88,"props":3142,"children":3143},{"style":179},[3144],{"type":34,"value":3145},"warn",{"type":25,"tag":88,"props":3147,"children":3148},{"style":95},[3149],{"type":34,"value":367},{"type":25,"tag":88,"props":3151,"children":3152},{"style":107},[3153],{"type":34,"value":3154},"\"Failed login attempt for user: {}\"",{"type":25,"tag":88,"props":3156,"children":3157},{"style":95},[3158],{"type":34,"value":2501},{"type":25,"tag":30,"props":3160,"children":3161},{},[3162],{"type":34,"value":3163},"Aggregate logs centrally, and set up alerts for suspicious activities.",{"type":25,"tag":52,"props":3165,"children":3166},{"id":851},[3167],{"type":34,"value":854},{"type":25,"tag":30,"props":3169,"children":3170},{},[3171],{"type":34,"value":3172},"Adopting these best practices can greatly enhance the security posture of your Java applications. Stay proactive by regularly reviewing security measures, updating dependencies, and monitoring your systems. Security is a continuous process, not a one-time setup.",{"type":25,"tag":861,"props":3174,"children":3175},{"id":863},[3176],{"type":34,"value":866},{"type":25,"tag":392,"props":3178,"children":3179},{},[3180,3190,3200],{"type":25,"tag":396,"props":3181,"children":3182},{},[3183],{"type":25,"tag":874,"props":3184,"children":3187},{"href":3185,"rel":3186},"https://owasp.org/www-project-top-ten/",[878],[3188],{"type":34,"value":3189},"OWASP Top 10",{"type":25,"tag":396,"props":3191,"children":3192},{},[3193],{"type":25,"tag":874,"props":3194,"children":3197},{"href":3195,"rel":3196},"https://spring.io/projects/spring-security",[878],[3198],{"type":34,"value":3199},"Spring Security",{"type":25,"tag":396,"props":3201,"children":3202},{},[3203],{"type":25,"tag":874,"props":3204,"children":3207},{"href":3205,"rel":3206},"https://owasp.org/www-project-dependency-check/",[878],[3208],{"type":34,"value":3209},"OWASP Dependency Check",{"type":25,"tag":893,"props":3211,"children":3212},{},[3213],{"type":34,"value":897},{"title":7,"searchDepth":118,"depth":118,"links":3215},[3216,3217,3218,3219,3220,3221,3222,3223,3224,3225],{"id":2354,"depth":118,"text":2357},{"id":2424,"depth":118,"text":2427},{"id":2560,"depth":118,"text":2563},{"id":2671,"depth":118,"text":2674},{"id":2810,"depth":118,"text":2813},{"id":2851,"depth":118,"text":2854},{"id":2883,"depth":118,"text":2886},{"id":2981,"depth":118,"text":2984},{"id":3117,"depth":118,"text":3120},{"id":851,"depth":118,"text":854},"content:posts:java-security-best-practices.md","posts/java-security-best-practices.md","posts/java-security-best-practices",{"_path":3230,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":3231,"description":3232,"date":3233,"layout":11,"subtitle":3234,"image":3235,"optimized_image":3235,"category":14,"tags":3236,"author":20,"paginate":6,"body":3240,"_type":912,"_id":4040,"_source":914,"_file":4041,"_stem":4042,"_extension":917},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","An expert guide to understanding and implementing circuit breakers in distributed systems, with practical Java examples using Resilience4j and best practices.","2024-07-15T11:00:00.000Z","Enhancing Resilience with Circuit Breaker Patterns","/assets/img/uploads/circuit-breaking-resilience.png",[14,16,3237,3238,3239],"resilience","circuit-breaker","distributed-systems",{"type":22,"children":3241,"toc":4028},[3242,3250,3256,3261,3294,3299,3305,3310,3328,3334,3340,3345,3455,3461,3665,3671,3676,3839,3845,3850,3944,3949,3955,3978,3982,3987,3991,4024],{"type":25,"tag":26,"props":3243,"children":3244},{},[3245],{"type":25,"tag":30,"props":3246,"children":3247},{},[3248],{"type":34,"value":3249},"In modern distributed systems, failures are inevitable. The circuit breaker pattern helps maintain system stability by preventing failures from cascading across service boundaries. In this post, we'll dive deep into circuit breakers, explore their importance, and demonstrate practical implementation using Resilience4j in Java.",{"type":25,"tag":52,"props":3251,"children":3253},{"id":3252},"understanding-the-circuit-breaker-pattern",[3254],{"type":34,"value":3255},"Understanding the Circuit Breaker Pattern",{"type":25,"tag":30,"props":3257,"children":3258},{},[3259],{"type":34,"value":3260},"The circuit breaker pattern prevents a service from making repeated calls to an external service that is experiencing issues. It has three main states:",{"type":25,"tag":392,"props":3262,"children":3263},{},[3264,3274,3284],{"type":25,"tag":396,"props":3265,"children":3266},{},[3267,3272],{"type":25,"tag":37,"props":3268,"children":3269},{},[3270],{"type":34,"value":3271},"Closed:",{"type":34,"value":3273}," All requests pass through normally.",{"type":25,"tag":396,"props":3275,"children":3276},{},[3277,3282],{"type":25,"tag":37,"props":3278,"children":3279},{},[3280],{"type":34,"value":3281},"Open:",{"type":34,"value":3283}," Requests fail fast, bypassing the problematic service.",{"type":25,"tag":396,"props":3285,"children":3286},{},[3287,3292],{"type":25,"tag":37,"props":3288,"children":3289},{},[3290],{"type":34,"value":3291},"Half-Open:",{"type":34,"value":3293}," A limited number of requests test if the external service has recovered.",{"type":25,"tag":30,"props":3295,"children":3296},{},[3297],{"type":34,"value":3298},"This mechanism helps improve the resilience and responsiveness of your systems.",{"type":25,"tag":52,"props":3300,"children":3302},{"id":3301},"when-to-use-circuit-breakers",[3303],{"type":34,"value":3304},"When to Use Circuit Breakers",{"type":25,"tag":30,"props":3306,"children":3307},{},[3308],{"type":34,"value":3309},"Circuit breakers are essential when:",{"type":25,"tag":392,"props":3311,"children":3312},{},[3313,3318,3323],{"type":25,"tag":396,"props":3314,"children":3315},{},[3316],{"type":34,"value":3317},"Your system relies on remote calls (microservices architecture, external APIs).",{"type":25,"tag":396,"props":3319,"children":3320},{},[3321],{"type":34,"value":3322},"You need to prevent failure propagation.",{"type":25,"tag":396,"props":3324,"children":3325},{},[3326],{"type":34,"value":3327},"You aim to improve user experience by quickly responding to failures.",{"type":25,"tag":52,"props":3329,"children":3331},{"id":3330},"implementing-circuit-breakers-with-resilience4j",[3332],{"type":34,"value":3333},"Implementing Circuit Breakers with Resilience4j",{"type":25,"tag":72,"props":3335,"children":3337},{"id":3336},"step-1-adding-dependencies",[3338],{"type":34,"value":3339},"Step 1: Adding Dependencies",{"type":25,"tag":30,"props":3341,"children":3342},{},[3343],{"type":34,"value":3344},"Add Resilience4j dependencies to your project (Maven example):",{"type":25,"tag":79,"props":3346,"children":3348},{"className":2992,"code":3347,"language":2994,"meta":7,"style":7},"\u003Cdependency>\n    \u003CgroupId>io.github.resilience4j\u003C/groupId>\n    \u003CartifactId>resilience4j-circuitbreaker\u003C/artifactId>\n    \u003Cversion>2.0.2\u003C/version>\n\u003C/dependency>\n",[3349],{"type":25,"tag":14,"props":3350,"children":3351},{"__ignoreMap":7},[3352,3368,3392,3416,3440],{"type":25,"tag":88,"props":3353,"children":3354},{"class":90,"line":91},[3355,3359,3364],{"type":25,"tag":88,"props":3356,"children":3357},{"style":95},[3358],{"type":34,"value":3006},{"type":25,"tag":88,"props":3360,"children":3361},{"style":3009},[3362],{"type":34,"value":3363},"dependency",{"type":25,"tag":88,"props":3365,"children":3366},{"style":95},[3367],{"type":34,"value":3017},{"type":25,"tag":88,"props":3369,"children":3370},{"class":90,"line":118},[3371,3375,3379,3384,3388],{"type":25,"tag":88,"props":3372,"children":3373},{"style":95},[3374],{"type":34,"value":3025},{"type":25,"tag":88,"props":3376,"children":3377},{"style":3009},[3378],{"type":34,"value":3030},{"type":25,"tag":88,"props":3380,"children":3381},{"style":95},[3382],{"type":34,"value":3383},">io.github.resilience4j\u003C/",{"type":25,"tag":88,"props":3385,"children":3386},{"style":3009},[3387],{"type":34,"value":3030},{"type":25,"tag":88,"props":3389,"children":3390},{"style":95},[3391],{"type":34,"value":3017},{"type":25,"tag":88,"props":3393,"children":3394},{"class":90,"line":128},[3395,3399,3403,3408,3412],{"type":25,"tag":88,"props":3396,"children":3397},{"style":95},[3398],{"type":34,"value":3025},{"type":25,"tag":88,"props":3400,"children":3401},{"style":3009},[3402],{"type":34,"value":3055},{"type":25,"tag":88,"props":3404,"children":3405},{"style":95},[3406],{"type":34,"value":3407},">resilience4j-circuitbreaker\u003C/",{"type":25,"tag":88,"props":3409,"children":3410},{"style":3009},[3411],{"type":34,"value":3055},{"type":25,"tag":88,"props":3413,"children":3414},{"style":95},[3415],{"type":34,"value":3017},{"type":25,"tag":88,"props":3417,"children":3418},{"class":90,"line":152},[3419,3423,3427,3432,3436],{"type":25,"tag":88,"props":3420,"children":3421},{"style":95},[3422],{"type":34,"value":3025},{"type":25,"tag":88,"props":3424,"children":3425},{"style":3009},[3426],{"type":34,"value":3080},{"type":25,"tag":88,"props":3428,"children":3429},{"style":95},[3430],{"type":34,"value":3431},">2.0.2\u003C/",{"type":25,"tag":88,"props":3433,"children":3434},{"style":3009},[3435],{"type":34,"value":3080},{"type":25,"tag":88,"props":3437,"children":3438},{"style":95},[3439],{"type":34,"value":3017},{"type":25,"tag":88,"props":3441,"children":3442},{"class":90,"line":170},[3443,3447,3451],{"type":25,"tag":88,"props":3444,"children":3445},{"style":95},[3446],{"type":34,"value":3101},{"type":25,"tag":88,"props":3448,"children":3449},{"style":3009},[3450],{"type":34,"value":3363},{"type":25,"tag":88,"props":3452,"children":3453},{"style":95},[3454],{"type":34,"value":3017},{"type":25,"tag":72,"props":3456,"children":3458},{"id":3457},"step-2-configuring-circuit-breaker",[3459],{"type":34,"value":3460},"Step 2: Configuring Circuit Breaker",{"type":25,"tag":79,"props":3462,"children":3464},{"className":81,"code":3463,"language":16,"meta":7,"style":7},"CircuitBreakerConfig config = CircuitBreakerConfig.custom()\n    .failureRateThreshold(50)\n    .waitDurationInOpenState(Duration.ofMillis(1000))\n    .permittedNumberOfCallsInHalfOpenState(2)\n    .minimumNumberOfCalls(5)\n    .build();\n\nCircuitBreaker circuitBreaker = CircuitBreaker.of(\"myService\", config);\n",[3465],{"type":25,"tag":14,"props":3466,"children":3467},{"__ignoreMap":7},[3468,3495,3521,3557,3581,3606,3622,3629],{"type":25,"tag":88,"props":3469,"children":3470},{"class":90,"line":91},[3471,3476,3480,3485,3490],{"type":25,"tag":88,"props":3472,"children":3473},{"style":95},[3474],{"type":34,"value":3475},"CircuitBreakerConfig config ",{"type":25,"tag":88,"props":3477,"children":3478},{"style":101},[3479],{"type":34,"value":104},{"type":25,"tag":88,"props":3481,"children":3482},{"style":95},[3483],{"type":34,"value":3484}," CircuitBreakerConfig.",{"type":25,"tag":88,"props":3486,"children":3487},{"style":179},[3488],{"type":34,"value":3489},"custom",{"type":25,"tag":88,"props":3491,"children":3492},{"style":95},[3493],{"type":34,"value":3494},"()\n",{"type":25,"tag":88,"props":3496,"children":3497},{"class":90,"line":118},[3498,3503,3508,3512,3517],{"type":25,"tag":88,"props":3499,"children":3500},{"style":95},[3501],{"type":34,"value":3502},"    .",{"type":25,"tag":88,"props":3504,"children":3505},{"style":179},[3506],{"type":34,"value":3507},"failureRateThreshold",{"type":25,"tag":88,"props":3509,"children":3510},{"style":95},[3511],{"type":34,"value":367},{"type":25,"tag":88,"props":3513,"children":3514},{"style":333},[3515],{"type":34,"value":3516},"50",{"type":25,"tag":88,"props":3518,"children":3519},{"style":95},[3520],{"type":34,"value":1194},{"type":25,"tag":88,"props":3522,"children":3523},{"class":90,"line":128},[3524,3528,3533,3538,3543,3547,3552],{"type":25,"tag":88,"props":3525,"children":3526},{"style":95},[3527],{"type":34,"value":3502},{"type":25,"tag":88,"props":3529,"children":3530},{"style":179},[3531],{"type":34,"value":3532},"waitDurationInOpenState",{"type":25,"tag":88,"props":3534,"children":3535},{"style":95},[3536],{"type":34,"value":3537},"(Duration.",{"type":25,"tag":88,"props":3539,"children":3540},{"style":179},[3541],{"type":34,"value":3542},"ofMillis",{"type":25,"tag":88,"props":3544,"children":3545},{"style":95},[3546],{"type":34,"value":367},{"type":25,"tag":88,"props":3548,"children":3549},{"style":333},[3550],{"type":34,"value":3551},"1000",{"type":25,"tag":88,"props":3553,"children":3554},{"style":95},[3555],{"type":34,"value":3556},"))\n",{"type":25,"tag":88,"props":3558,"children":3559},{"class":90,"line":152},[3560,3564,3569,3573,3577],{"type":25,"tag":88,"props":3561,"children":3562},{"style":95},[3563],{"type":34,"value":3502},{"type":25,"tag":88,"props":3565,"children":3566},{"style":179},[3567],{"type":34,"value":3568},"permittedNumberOfCallsInHalfOpenState",{"type":25,"tag":88,"props":3570,"children":3571},{"style":95},[3572],{"type":34,"value":367},{"type":25,"tag":88,"props":3574,"children":3575},{"style":333},[3576],{"type":34,"value":2521},{"type":25,"tag":88,"props":3578,"children":3579},{"style":95},[3580],{"type":34,"value":1194},{"type":25,"tag":88,"props":3582,"children":3583},{"class":90,"line":170},[3584,3588,3593,3597,3602],{"type":25,"tag":88,"props":3585,"children":3586},{"style":95},[3587],{"type":34,"value":3502},{"type":25,"tag":88,"props":3589,"children":3590},{"style":179},[3591],{"type":34,"value":3592},"minimumNumberOfCalls",{"type":25,"tag":88,"props":3594,"children":3595},{"style":95},[3596],{"type":34,"value":367},{"type":25,"tag":88,"props":3598,"children":3599},{"style":333},[3600],{"type":34,"value":3601},"5",{"type":25,"tag":88,"props":3603,"children":3604},{"style":95},[3605],{"type":34,"value":1194},{"type":25,"tag":88,"props":3607,"children":3608},{"class":90,"line":200},[3609,3613,3618],{"type":25,"tag":88,"props":3610,"children":3611},{"style":95},[3612],{"type":34,"value":3502},{"type":25,"tag":88,"props":3614,"children":3615},{"style":179},[3616],{"type":34,"value":3617},"build",{"type":25,"tag":88,"props":3619,"children":3620},{"style":95},[3621],{"type":34,"value":651},{"type":25,"tag":88,"props":3623,"children":3624},{"class":90,"line":379},[3625],{"type":25,"tag":88,"props":3626,"children":3627},{"emptyLinePlaceholder":122},[3628],{"type":34,"value":125},{"type":25,"tag":88,"props":3630,"children":3631},{"class":90,"line":561},[3632,3637,3641,3646,3651,3655,3660],{"type":25,"tag":88,"props":3633,"children":3634},{"style":95},[3635],{"type":34,"value":3636},"CircuitBreaker circuitBreaker ",{"type":25,"tag":88,"props":3638,"children":3639},{"style":101},[3640],{"type":34,"value":104},{"type":25,"tag":88,"props":3642,"children":3643},{"style":95},[3644],{"type":34,"value":3645}," CircuitBreaker.",{"type":25,"tag":88,"props":3647,"children":3648},{"style":179},[3649],{"type":34,"value":3650},"of",{"type":25,"tag":88,"props":3652,"children":3653},{"style":95},[3654],{"type":34,"value":367},{"type":25,"tag":88,"props":3656,"children":3657},{"style":107},[3658],{"type":34,"value":3659},"\"myService\"",{"type":25,"tag":88,"props":3661,"children":3662},{"style":95},[3663],{"type":34,"value":3664},", config);\n",{"type":25,"tag":72,"props":3666,"children":3668},{"id":3667},"step-3-using-circuit-breaker",[3669],{"type":34,"value":3670},"Step 3: Using Circuit Breaker",{"type":25,"tag":30,"props":3672,"children":3673},{},[3674],{"type":34,"value":3675},"Wrap service calls using the circuit breaker:",{"type":25,"tag":79,"props":3677,"children":3679},{"className":81,"code":3678,"language":16,"meta":7,"style":7},"Supplier\u003CString> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> myService.remoteCall());\n\nTry.ofSupplier(decoratedSupplier)\n    .recover(throwable -> \"Fallback response\")\n    .onSuccess(result -> System.out.println(\"Response: \" + result));\n",[3680],{"type":25,"tag":14,"props":3681,"children":3682},{"__ignoreMap":7},[3683,3737,3744,3762,3792],{"type":25,"tag":88,"props":3684,"children":3685},{"class":90,"line":91},[3686,3691,3696,3701,3705,3709,3714,3719,3723,3728,3733],{"type":25,"tag":88,"props":3687,"children":3688},{"style":95},[3689],{"type":34,"value":3690},"Supplier\u003C",{"type":25,"tag":88,"props":3692,"children":3693},{"style":101},[3694],{"type":34,"value":3695},"String",{"type":25,"tag":88,"props":3697,"children":3698},{"style":95},[3699],{"type":34,"value":3700},"> decoratedSupplier ",{"type":25,"tag":88,"props":3702,"children":3703},{"style":101},[3704],{"type":34,"value":104},{"type":25,"tag":88,"props":3706,"children":3707},{"style":95},[3708],{"type":34,"value":3645},{"type":25,"tag":88,"props":3710,"children":3711},{"style":179},[3712],{"type":34,"value":3713},"decorateSupplier",{"type":25,"tag":88,"props":3715,"children":3716},{"style":95},[3717],{"type":34,"value":3718},"(circuitBreaker, () ",{"type":25,"tag":88,"props":3720,"children":3721},{"style":101},[3722],{"type":34,"value":275},{"type":25,"tag":88,"props":3724,"children":3725},{"style":95},[3726],{"type":34,"value":3727}," myService.",{"type":25,"tag":88,"props":3729,"children":3730},{"style":179},[3731],{"type":34,"value":3732},"remoteCall",{"type":25,"tag":88,"props":3734,"children":3735},{"style":95},[3736],{"type":34,"value":197},{"type":25,"tag":88,"props":3738,"children":3739},{"class":90,"line":118},[3740],{"type":25,"tag":88,"props":3741,"children":3742},{"emptyLinePlaceholder":122},[3743],{"type":34,"value":125},{"type":25,"tag":88,"props":3745,"children":3746},{"class":90,"line":128},[3747,3752,3757],{"type":25,"tag":88,"props":3748,"children":3749},{"style":95},[3750],{"type":34,"value":3751},"Try.",{"type":25,"tag":88,"props":3753,"children":3754},{"style":179},[3755],{"type":34,"value":3756},"ofSupplier",{"type":25,"tag":88,"props":3758,"children":3759},{"style":95},[3760],{"type":34,"value":3761},"(decoratedSupplier)\n",{"type":25,"tag":88,"props":3763,"children":3764},{"class":90,"line":152},[3765,3769,3774,3779,3783,3788],{"type":25,"tag":88,"props":3766,"children":3767},{"style":95},[3768],{"type":34,"value":3502},{"type":25,"tag":88,"props":3770,"children":3771},{"style":179},[3772],{"type":34,"value":3773},"recover",{"type":25,"tag":88,"props":3775,"children":3776},{"style":95},[3777],{"type":34,"value":3778},"(throwable ",{"type":25,"tag":88,"props":3780,"children":3781},{"style":101},[3782],{"type":34,"value":275},{"type":25,"tag":88,"props":3784,"children":3785},{"style":107},[3786],{"type":34,"value":3787}," \"Fallback response\"",{"type":25,"tag":88,"props":3789,"children":3790},{"style":95},[3791],{"type":34,"value":1194},{"type":25,"tag":88,"props":3793,"children":3794},{"class":90,"line":170},[3795,3799,3804,3809,3813,3817,3821,3825,3830,3834],{"type":25,"tag":88,"props":3796,"children":3797},{"style":95},[3798],{"type":34,"value":3502},{"type":25,"tag":88,"props":3800,"children":3801},{"style":179},[3802],{"type":34,"value":3803},"onSuccess",{"type":25,"tag":88,"props":3805,"children":3806},{"style":95},[3807],{"type":34,"value":3808},"(result ",{"type":25,"tag":88,"props":3810,"children":3811},{"style":101},[3812],{"type":34,"value":275},{"type":25,"tag":88,"props":3814,"children":3815},{"style":95},[3816],{"type":34,"value":280},{"type":25,"tag":88,"props":3818,"children":3819},{"style":179},[3820],{"type":34,"value":182},{"type":25,"tag":88,"props":3822,"children":3823},{"style":95},[3824],{"type":34,"value":367},{"type":25,"tag":88,"props":3826,"children":3827},{"style":107},[3828],{"type":34,"value":3829},"\"Response: \"",{"type":25,"tag":88,"props":3831,"children":3832},{"style":101},[3833],{"type":34,"value":796},{"type":25,"tag":88,"props":3835,"children":3836},{"style":95},[3837],{"type":34,"value":3838}," result));\n",{"type":25,"tag":52,"props":3840,"children":3842},{"id":3841},"monitoring-and-metrics",[3843],{"type":34,"value":3844},"Monitoring and Metrics",{"type":25,"tag":30,"props":3846,"children":3847},{},[3848],{"type":34,"value":3849},"Resilience4j provides built-in support for metrics and monitoring:",{"type":25,"tag":79,"props":3851,"children":3853},{"className":81,"code":3852,"language":16,"meta":7,"style":7},"CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();\nregistry.getEventPublisher()\n    .onStateTransition(event -> System.out.println(event.getStateTransition()));\n",[3854],{"type":25,"tag":14,"props":3855,"children":3856},{"__ignoreMap":7},[3857,3883,3900],{"type":25,"tag":88,"props":3858,"children":3859},{"class":90,"line":91},[3860,3865,3869,3874,3879],{"type":25,"tag":88,"props":3861,"children":3862},{"style":95},[3863],{"type":34,"value":3864},"CircuitBreakerRegistry registry ",{"type":25,"tag":88,"props":3866,"children":3867},{"style":101},[3868],{"type":34,"value":104},{"type":25,"tag":88,"props":3870,"children":3871},{"style":95},[3872],{"type":34,"value":3873}," CircuitBreakerRegistry.",{"type":25,"tag":88,"props":3875,"children":3876},{"style":179},[3877],{"type":34,"value":3878},"ofDefaults",{"type":25,"tag":88,"props":3880,"children":3881},{"style":95},[3882],{"type":34,"value":651},{"type":25,"tag":88,"props":3884,"children":3885},{"class":90,"line":118},[3886,3891,3896],{"type":25,"tag":88,"props":3887,"children":3888},{"style":95},[3889],{"type":34,"value":3890},"registry.",{"type":25,"tag":88,"props":3892,"children":3893},{"style":179},[3894],{"type":34,"value":3895},"getEventPublisher",{"type":25,"tag":88,"props":3897,"children":3898},{"style":95},[3899],{"type":34,"value":3494},{"type":25,"tag":88,"props":3901,"children":3902},{"class":90,"line":128},[3903,3907,3912,3917,3921,3925,3929,3934,3939],{"type":25,"tag":88,"props":3904,"children":3905},{"style":95},[3906],{"type":34,"value":3502},{"type":25,"tag":88,"props":3908,"children":3909},{"style":179},[3910],{"type":34,"value":3911},"onStateTransition",{"type":25,"tag":88,"props":3913,"children":3914},{"style":95},[3915],{"type":34,"value":3916},"(event ",{"type":25,"tag":88,"props":3918,"children":3919},{"style":101},[3920],{"type":34,"value":275},{"type":25,"tag":88,"props":3922,"children":3923},{"style":95},[3924],{"type":34,"value":280},{"type":25,"tag":88,"props":3926,"children":3927},{"style":179},[3928],{"type":34,"value":182},{"type":25,"tag":88,"props":3930,"children":3931},{"style":95},[3932],{"type":34,"value":3933},"(event.",{"type":25,"tag":88,"props":3935,"children":3936},{"style":179},[3937],{"type":34,"value":3938},"getStateTransition",{"type":25,"tag":88,"props":3940,"children":3941},{"style":95},[3942],{"type":34,"value":3943},"()));\n",{"type":25,"tag":30,"props":3945,"children":3946},{},[3947],{"type":34,"value":3948},"Integrate with Prometheus, Grafana, or other observability tools for comprehensive monitoring.",{"type":25,"tag":52,"props":3950,"children":3952},{"id":3951},"best-practices-for-circuit-breaking",[3953],{"type":34,"value":3954},"Best Practices for Circuit Breaking",{"type":25,"tag":392,"props":3956,"children":3957},{},[3958,3963,3968,3973],{"type":25,"tag":396,"props":3959,"children":3960},{},[3961],{"type":34,"value":3962},"Configure sensible thresholds to avoid premature circuit opening.",{"type":25,"tag":396,"props":3964,"children":3965},{},[3966],{"type":34,"value":3967},"Always define fallback mechanisms.",{"type":25,"tag":396,"props":3969,"children":3970},{},[3971],{"type":34,"value":3972},"Regularly monitor circuit breaker states and metrics.",{"type":25,"tag":396,"props":3974,"children":3975},{},[3976],{"type":34,"value":3977},"Test thoroughly to ensure reliability under failure conditions.",{"type":25,"tag":52,"props":3979,"children":3980},{"id":851},[3981],{"type":34,"value":854},{"type":25,"tag":30,"props":3983,"children":3984},{},[3985],{"type":34,"value":3986},"Implementing circuit breakers using Resilience4j significantly enhances the resilience of your Java-based distributed systems. This proactive approach ensures your systems gracefully handle failures, maintaining service availability and user satisfaction.",{"type":25,"tag":861,"props":3988,"children":3989},{"id":863},[3990],{"type":34,"value":866},{"type":25,"tag":392,"props":3992,"children":3993},{},[3994,4004,4014],{"type":25,"tag":396,"props":3995,"children":3996},{},[3997],{"type":25,"tag":874,"props":3998,"children":4001},{"href":3999,"rel":4000},"https://resilience4j.readme.io/docs",[878],[4002],{"type":34,"value":4003},"Resilience4j Official Documentation",{"type":25,"tag":396,"props":4005,"children":4006},{},[4007],{"type":25,"tag":874,"props":4008,"children":4011},{"href":4009,"rel":4010},"https://martinfowler.com/bliki/CircuitBreaker.html",[878],[4012],{"type":34,"value":4013},"Circuit Breaker Pattern by Martin Fowler",{"type":25,"tag":396,"props":4015,"children":4016},{},[4017],{"type":25,"tag":874,"props":4018,"children":4021},{"href":4019,"rel":4020},"https://microservices.io/patterns/reliability/circuit-breaker.html",[878],[4022],{"type":34,"value":4023},"Microservices Patterns by Chris Richardson",{"type":25,"tag":893,"props":4025,"children":4026},{},[4027],{"type":34,"value":897},{"title":7,"searchDepth":118,"depth":118,"links":4029},[4030,4031,4032,4037,4038,4039],{"id":3252,"depth":118,"text":3255},{"id":3301,"depth":118,"text":3304},{"id":3330,"depth":118,"text":3333,"children":4033},[4034,4035,4036],{"id":3336,"depth":128,"text":3339},{"id":3457,"depth":128,"text":3460},{"id":3667,"depth":128,"text":3670},{"id":3841,"depth":118,"text":3844},{"id":3951,"depth":118,"text":3954},{"id":851,"depth":118,"text":854},"content:posts:circuit-breaking-resilience.md","posts/circuit-breaking-resilience.md","posts/circuit-breaking-resilience",[4044,4045,4046,4047,4051,4052,4056,4060,4064,4068,4072,4076,4080,4084,4088,4092,4096,4100,4103,4107,4111],{"_path":920,"title":921,"date":923},{"_path":2332,"title":2333,"date":2335},{"_path":3230,"title":3231,"date":3233},{"_path":4048,"title":4049,"date":4050},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":4,"title":8,"date":10},{"_path":4053,"title":4054,"date":4055},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":4057,"title":4058,"date":4059},"/posts/mastering-concurrency","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","2024-03-15T11:00:00.000Z",{"_path":4061,"title":4062,"date":4063},"/posts/microservices-mistakes","Common Mistakes When Developing Microservices","2024-02-15T11:00:00.000Z",{"_path":4065,"title":4066,"date":4067},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":4069,"title":4070,"date":4071},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":4073,"title":4074,"date":4075},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":4077,"title":4078,"date":4079},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":4081,"title":4082,"date":4083},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":4085,"title":4086,"date":4087},"/posts/python-virtual-environments","Python Virtual Environments (venv)","2023-09-17 22:00:00",{"_path":4089,"title":4090,"date":4091},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":4093,"title":4094,"date":4095},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":4097,"title":4098,"date":4099},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","2020-12-06T11:00:00.000Z",{"_path":4101,"title":4102,"date":4099},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot",{"_path":4104,"title":4105,"date":4106},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":4108,"title":4109,"date":4110},"/posts/algorithms-working-with-trees","Algorithms - Binary tree traversals in kotlin","2020-11-24T11:00:00.000Z",{"_path":4112,"title":4113,"date":4114},"/posts/playing-with-ocr-libraries","Playing with OCR libraries","2020-11-22T11:00:00.000Z",1790510065738]