@@ -118,7 +118,8 @@ To that end, we use the "Shadow Jar" plugin.
118118
119119Here's our final ` build.gradle ` :
120120
121- { /* {{< highlight gradle "linenos=table" >}} */ } { /* mdx-disabled */ }
121+ { /* {{< highlight gradle "linenos=table" >}} */ } { /* mdx-disabled */ } { /* TODO: packing this into a bash block for now */ }
122+ ``` gradle
122123plugins {
123124 id "java"
124125 id "org.jetbrains.kotlin.jvm" version '1.5.31'
@@ -195,6 +196,7 @@ shadowJar {
195196}
196197
197198assemble.dependsOn shadowJar
199+ ```
198200{ /* {/* {/* {/* {/* {/* {/* {/* {/* {/* {{< / highlight >}} */ } { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ }
199201
200202Please note that we will be using ` org.localstack.sampleproject ` as a
@@ -211,18 +213,18 @@ In this project, we are following
211213to setup up ` src/main/resources/log4j2.xml ` content.
212214
213215``` xml
214- {/* < ?xml version="1.0" encoding="UTF-8"?> */} {/* mdx-disabled */}
215- {/* <Configuration packages="com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender"> */} {/* mdx-disabled */}
216+ ?xml version="1.0" encoding="UTF-8"?>
217+ <Configuration packages =" com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender" >
216218 <Appenders >
217- {/* <Lambda name="Lambda"> */} {/* mdx-disabled */}
219+ <Lambda name =" Lambda" >
218220 <PatternLayout >
219221 <pattern >%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern >
220222 </PatternLayout >
221223 </Lambda >
222224 </Appenders >
223225 <Loggers >
224- {/* <Root level="debug"> */} {/* mdx-disabled */}
225- {/* <AppenderRef ref="Lambda" /> */} {/* mdx-disabled */}
226+ <Root level =" debug" >
227+ <AppenderRef ref =" Lambda" />
226228 </Root >
227229 </Loggers >
228230</Configuration >
@@ -256,6 +258,8 @@ Now our application needs an entry-class, the one we referenced earlier.
256258Let's add it under ` src/main/kotlin/org/localstack/sampleproject/Application.kt ` .
257259
258260{ /* {/* {/* {/* {/* {/* {/* {/* {/* {{< highlight kotlin "linenos=table" >}} */ } { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ } * /} { /* mdx-disabled */ }
261+ { /* TODO: packing this into a java block for now */ }
262+ ``` java
259263package org.localstack.sampleproject
260264
261265import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -266,7 +270,8 @@ class Application
266270fun main(args: Array<String>) {
267271 // Do nothing unless you use a custom runtime
268272}
269- {{< / highlight >}}
273+ ```
274+ { /* {{< / highlight >}}*/ }
270275
271276### Configure Jackson
272277
@@ -275,7 +280,9 @@ The easiest way to get started with JSON is to use the Jackson library.
275280Let's configure it by creating a new configuration class ` JacksonConfiguration.kt ` under
276281` src/main/kotlin/org/localstack/sampleproject/config ` :
277282
278- {{< highlight kotlin "linenos=table" >}}
283+ { /* {{< highlight kotlin "linenos=table" >}}*/ }
284+
285+ ``` java
279286package org.localstack.sampleproject.config
280287
281288import com.fasterxml.jackson.annotation.JsonInclude
@@ -306,8 +313,8 @@ class JacksonConfiguration {
306313 findAndRegisterModules()
307314 }
308315}
309-
310- {{ < / highlight >}}
316+ ```
317+ { /* {{ < / highlight >}} */ }
311318
312319In applications where you need support for multiple formats or a format
313320different from JSON (for example, SOAP/XML applications) simply use multiple
@@ -319,7 +326,9 @@ implementations.
319326
320327Let's create a small logging utility to simplify interactions with the logger
321328
322- { {< highlight kotlin " linenos=table" >}}
329+ { /* {{< highlight kotlin "linenos=table" >}}*/ }
330+ { /* TODO: packing this into a java block for now */ }
331+ ``` java
323332package org.localstack.sampleproject.util
324333
325334import org.apache.logging.log4j.LogManager
@@ -328,7 +337,8 @@ import org.apache.logging.log4j.Logger
328337open class Logger {
329338 val LOGGER : Logger = LogManager.getLogger (javaClass .enclosingClass )
330339}
331- {{< / highlight > }}
340+ ```
341+ {/* {{< / highlight >}}*/ }
332342
333343### Add Request / Response utilities
334344
@@ -343,7 +353,9 @@ Your application may even support multiple protocols with different request/resp
343353
344354Let ' s define utility functions to to build API gateway responses:
345355
346- {{< highlight kotlin " linenos=table" >}}
356+ {/*{{< highlight kotlin "linenos=table" >}}*/}
357+ {/* TODO: packing this into a java block for now */}
358+ ```java
347359package org.localstack.sampleproject.util
348360
349361import org.springframework.messaging.Message
@@ -365,11 +377,14 @@ fun <T>buildJsonResponse(data: T, code: Int = 200): Message<T> {
365377
366378fun buildJsonErrorResponse(message: String, code: Int = 500) =
367379 buildJsonResponse(ResponseError(message), code)
368- { {< / highlight >}}
380+ ```
381+ {/*{{< / highlight >}}*/}
369382
370383And now a utility function to process API Gateway requests:
371384
372- { {< highlight kotlin " linenos=table" >}}
385+ {/*{{< highlight kotlin "linenos=table" >}}*/}
386+ {/* TODO: packing this into a java block for now */}
387+ ```java
373388package org.localstack.sampleproject.util
374389
375390import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
@@ -393,15 +408,18 @@ fun <T>apiGatewayFunction(
393408 return@Function buildJsonErrorResponse(message ?: " " , 500)
394409 }
395410}
396- {{< / highlight > }}
411+ ```
412+ {/*{{< / highlight >}}*/}
397413
398414### Creating a sample Model / DTO
399415
400416To transfer data from requests into something more meaningful than JSON strings
401417(and back) you will be using a lot of Models and Data Transfer Objects (DTOs).
402418It's time to define our first one.
403419
404- {{< highlight kotlin " linenos=table" >}}
420+ {/*{{< highlight kotlin " linenos= table" >}}*/}
421+ {/* TODO: packing this into a java block for now */}
422+ ```java
405423package org.localstack.sampleproject.model
406424
407425import com.fasterxml.jackson.annotation.JsonIgnore
@@ -413,14 +431,17 @@ data class SampleModel(
413431 @JsonIgnore
414432 val jsonIgnoredProperty: String? = null,
415433)
416- { {< / highlight >}}
434+ ```
435+ {/*{{< / highlight >}}*/}
417436
418437### Creating Rest API endpoints
419438
420439Let's add our first endpoints to simulate CRUD operations on previously
421440defined `SampleModel`:
422441
423- { {< highlight kotlin " linenos=table" >}}
442+ {/*{{< highlight kotlin " linenos= table" >}}*/}
443+ {/* TODO: packing this into a java block for now */}
444+ ```java
424445package org.localstack.sampleproject.api
425446
426447import com.fasterxml.jackson.databind.ObjectMapper
@@ -461,7 +482,9 @@ class SampleApi(private val objectMapper: ObjectMapper) {
461482 buildJsonResponse(SAMPLE_RESPONSE.find { it.id == desiredId })
462483 }
463484}
464- {{< / highlight >}}
485+
486+ ```
487+ {/*{{< / highlight >}}*/}
465488
466489Note how we used Spring's dependency injection to inject `ObjectMapper` Bean we
467490configured earlier.
@@ -472,7 +495,9 @@ We know Java's cold start is always a pain.
472495To minimize this pain, we will try to define a pre-warming endpoint within the Rest API.
473496By invoking this function every 5-10 mins we can make sure Rest API lambda is always kept in a pre-warmed state.
474497
475- {{< highlight kotlin " linenos=table" >}}
498+ {/*{{< highlight kotlin " linenos= table" >}}*/}
499+ {/* TODO: packing this into a java block for now */}
500+ ```java
476501package org.localstack.sampleproject.api
477502
478503import com.fasterxml.jackson.databind.ObjectMapper
@@ -490,7 +515,9 @@ class ScheduleApi(private val objectMapper: ObjectMapper) {
490515 buildJsonResponse(" OK " )
491516 }
492517}
493- {{< / highlight > }}
518+
519+ ```
520+ {/*{{< / highlight >}}*/}
494521
495522Now you can add a scheduled event to the Rest API lambda function with the following synthetic payload (to simulate API gateway request).
496523This way, you can define any other scheduled events, but we recommend using pure lambda functions.
@@ -513,7 +540,9 @@ We can still define pure lambda functions, DynamoDB stream handlers, and so on.
513540
514541Below you can find a little example of few lambda functions grouped in `LambdaApi` class.
515542
516- {{< highlight kotlin "linenos=table" >}}
543+ {/*{{< highlight kotlin " linenos= table" >}}*/}
544+ {/* TODO: packing this into a java block for now */}
545+ ```java
517546package org.localstack.sampleproject.api
518547
519548import com.amazonaws.services.lambda.runtime.events.DynamodbEvent
@@ -552,7 +581,9 @@ class LambdaApi : SpringBootStreamHandler() {
552581 }
553582 }
554583}
555- {{< / highlight >}}
584+
585+ ```
586+ {/*{{< / highlight >}}*/}
556587
557588As you can see from the example above, we are using `SpringBootStreamHandler`
558589class as a base that takes care of the application bootstrapping process and
@@ -572,6 +603,7 @@ for usage examples.
572603
573604{/* {{< tabpane >}} */} {/* mdx-disabled */}
574605{/* {{< tab header=" Serverless " lang=" yaml" >}} */} {/* mdx-disabled */}
606+ ```yaml
575607service: localstack-sampleproject-serverless
576608
577609provider:
@@ -621,8 +653,10 @@ functions:
621653 handler: org.localstack.sampleproject.api.LambdaApi
622654 environment:
623655 FUNCTION_NAME: functionTwo
656+ ```yaml
624657{/* {/* {/* {{< /tab >}} */} {/* mdx-disabled */} */} {/* mdx-disabled */} */} {/* mdx-disabled */}
625658{/* {{< tab header=" AWS CDK " lang=" kotlin" >}} */} {/* mdx-disabled */}
659+ ```java
626660package org.localstack.cdkstack
627661
628662import java.util.UUID
@@ -845,7 +879,7 @@ resource "aws_lambda_function" "exampleFunctionTwo" {
845879}
846880{{< / tab > }}
847881{/* {{< /tabpane >}} */ } {/* mdx-disabled */ }
848-
882+ ```
849883## Testing , Debugging and Hot Reloading
850884
851885Please read our [Lambda Tools ](/ content/ en/ tools/ lambda- tools/ _index. md)
0 commit comments