essobedo commented on issue #4124:
URL: https://github.com/apache/camel-k/issues/4124#issuecomment-1488892624

   OK so the link was helpful, I could finally make it work in native mode, and 
it allowed me to realize that some improvements could be made in the Java DSL 
support in camel-quarkus.
   
   Description of the changes made in your project `issue4124part2` 
https://github.com/essobedo/issue4124part2/commit/796b1c52d4848f385228fcd92b6e98098f5260af:
   
   * Add the Jandex plugin to the pom file to allow Quarkus to find the 
annotations (Annotations in the integration class are ignored by Quarkus for 
now)
   * Add the annotation `RegisterForReflection` to all the classes that are 
accessed by reflection (by Jackson or camel-bean)
   * Configure the Mapper to be able to inject it thanks to CDI (Using 
`Mappers.getMapper(Class)` is not recommended for the native mode as it uses 
reflection see 
https://mapstruct.org/news/2019-12-06-mapstruct-and-quarkus/#summing-up)
   * Rewrite `IntegrationBean` to make it compatible with the native mode
   
   The Integration class:
   
   ```
   // camel-k: language=java
   // camel-k: name=issue4124-part1-integration
   
   import com.fasterxml.jackson.annotation.JsonInclude;
   import com.fasterxml.jackson.databind.DeserializationFeature;
   import com.fasterxml.jackson.databind.ObjectMapper;
   import com.fasterxml.jackson.databind.SerializationFeature;
   import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
   import com.mertdotcc.sources.IntegrationBean;
   import org.apache.camel.LoggingLevel;
   import org.apache.camel.builder.RouteBuilder;
   import org.apache.camel.component.jackson.JacksonDataFormat;
   import com.mertdotcc.mappingresources.source.Student;
   
   public class Integration extends RouteBuilder {
   
       @Override
       public void configure() throws Exception {
           onException(Exception.class)
                   .handled(true)
                   .setHeader("CamelHttpResponseCode").constant("400")
                   
.setBody().simple("resource:file:/etc/camel/resources/sample-error-response.json")
                   .log(LoggingLevel.ERROR, "exception", 
"${exception}").routeId("exception");
   
           ObjectMapper mapper = new ObjectMapper();
           mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 
false);
           mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
           mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
           mapper.registerModule(new JavaTimeModule());
           JacksonDataFormat jacksonObjectMapperFormat = new 
JacksonDataFormat(Student.class);
           jacksonObjectMapperFormat.setObjectMapper(mapper);                
   
           // POST
           from("direct://addStudent").routeId("addStudent")
                   .log(LoggingLevel.INFO, "before-mapping", "headers: 
${headers}; body: ${body}")
                   .removeHeaders("*")
                   .convertBodyTo(String.class)
                   .unmarshal(jacksonObjectMapperFormat)
                   .to("bean:IntegrationBean?method=handleMapping")
                   .marshal(jacksonObjectMapperFormat) 
                   .log(LoggingLevel.INFO, "after-mapping", "headers: 
${headers}; body: ${body}");
       }   
   }
   ```
   
   Modifications:
   
   * Configure the `JacksonDataFormat`
   * Use the component `camel-bean` to call `handleMapping`
   
   The run script:
   
   ```
   kamel run \
   --open-api file:./resources/part1-openapi.yaml \
   --resource file:./resources/sample-error-response.json \
   --dependency github:essobedo/issue4124part2 \
   --dependency camel:jackson \
   --trait container.enabled=true \
   --trait container.service-port=8443 \
   --trait container.request-cpu="250m" \
   --trait container.request-memory="256Mi" \
   --trait container.limit-cpu="500m" \
   --trait container.limit-memory="512Mi" \
   --trait health.enabled=true \
   --trait health.liveness-probe-enabled=true \
   --trait health.liveness-scheme="HTTP" \
   --trait health.liveness-initial-delay=0 \
   --trait health.liveness-timeout=10 \
   --trait health.liveness-period=15 \
   --trait health.liveness-success-threshold=1 \
   --trait health.liveness-failure-threshold=3 \
   --trait health.readiness-probe-enabled=true \
   --trait health.readiness-scheme="HTTP" \
   --trait health.readiness-initial-delay=0 \
   --trait health.readiness-timeout=10 \
   --trait health.readiness-period=15 \
   --trait health.readiness-success-threshold=1 \
   --trait health.readiness-failure-threshold=3 \
   --trait quarkus.enabled=true \
   --trait quarkus.package-type=native \
   Integration.java
   ```
   
   Modifications:
   * Use the trait option `container.service-port` to configure the port
   * Add explicitly the `jackson` component since Camel-K cannot detect it when 
using a custom configuration
   * Remove `mapstruct` from the direct dependency since it is not used by the 
integration class
   
   ## Result
   
   ```
   2023-03-29 15:32:19,190 INFO  [org.apa.cam.k.Runtime] (main) Apache Camel K 
Runtime 1.17.0
   2023-03-29 15:32:19,191 INFO  [org.apa.cam.qua.cor.CamelBootstrapRecorder] 
(main) Bootstrap runtime: org.apache.camel.quarkus.main.CamelMainRuntime
   2023-03-29 15:32:19,191 INFO  [org.apa.cam.mai.MainSupport] (main) Apache 
Camel (Main) 3.20.1 is starting
   2023-03-29 15:32:19,194 INFO  [org.apa.cam.k.lis.SourcesConfigurer] (main) 
Loading routes from: SourceDefinition{name='part1-openapi', language='xml', 
type='source', location='file:/etc/camel/sources/part1-openapi.xml', }
   2023-03-29 15:32:19,207 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] 
(main) Apache Camel 3.20.1 (camel-1) is starting
   2023-03-29 15:32:19,208 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] 
(main) Routes startup (started:2)
   2023-03-29 15:32:19,208 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] 
(main)     Started addStudent (direct://addStudent)
   2023-03-29 15:32:19,208 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] 
(main)     Started route1 (rest://post:/student)
   2023-03-29 15:32:19,208 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] 
(main) Apache Camel 3.20.1 (camel-1) started in 3ms (build:0ms init:3ms 
start:0ms)
   2023-03-29 15:32:19,264 INFO  [io.quarkus] (main) camel-k-integration 1.12.0 
native (powered by Quarkus 2.16.0.Final) started in 0.102s. Listening on: 
http://0.0.0.0:8080
   2023-03-29 15:32:19,264 INFO  [io.quarkus] (main) Profile prod activated. 
   2023-03-29 15:32:19,264 INFO  [io.quarkus] (main) Installed features: 
[camel-attachments, camel-bean, camel-core, camel-direct, camel-jackson, 
camel-java-joor-dsl, camel-k-core, camel-k-runtime, camel-kubernetes, 
camel-microprofile-health, camel-platform-http, camel-rest, camel-xml-io-dsl, 
cdi, kubernetes-client, security, smallrye-context-propagation, 
smallrye-health, vertx]
   2023-03-29 15:35:10,158 INFO  [before-mapping] (executor-thread-1) headers: 
{Accept=*/*, Accept-Encoding=gzip, deflate, br, CamelHttpMethod=POST, 
CamelHttpPath=/student, CamelHttpQuery=null, CamelHttpRawQuery=null, 
CamelHttpUri=/student, CamelHttpUrl=http://localhost:8080/student, 
CamelVertxPlatformHttpAuthenticatedUser=io.quarkus.vertx.http.runtime.security.QuarkusHttpUser@14dfb974,
 Connection=keep-alive, Content-Length=259, Content-Type=application/json, 
Host=localhost:8080, Postman-Token=be087bbf-a931-42ce-93b1-3c2a881a7818, 
User-Agent=PostmanRuntime/7.31.3}; body: {
       "studentFirstName": "Mert",
       "studentLastName": "Öztürk",
       "studentFaculty": "SCIENCE",
       "createdAt": "2016-09-06T13:37:50+03:00",
       "updatedAt": "2016-09-06T13:37:50+03:00",
       "grades": {
           "gpa": 3.50,
           "cgpa": 2.80
       }
   }
   
   student: Student{studentFirstName='Mert', studentLastName='Öztürk', 
studentFaculty=SCIENCE, createdAt=2016-09-06T10:37:50Z, 
updatedAt=2016-09-06T10:37:50Z, grades=StudentGrades{gpa=3.5, cgpa=2.8}}
   
   studentWithGrades: StudentWithGrades{studentFirstName='Mert', 
studentLastName='Öztürk', studentFaculty='SCIENCE', 
createdAt=2016-09-06T10:37:50Z, updatedAt=2016-09-06T10:37:50Z, gpa=3.5, 
cgpa=2.8}
   2023-03-29 15:35:10,164 INFO  [after-mapping] (executor-thread-1) headers: 
{Content-Type=application/json}; body: 
{"studentFirstName":"Mert","studentLastName":"Öztürk","studentFaculty":"SCIENCE","createdAt":"2016-09-06T10:37:50Z","updatedAt":"2016-09-06T10:37:50Z","gpa":3.5,"cgpa":2.8}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to