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