osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: BindingException: Cannot bind to json as message body is not json compatible.


Never-mind folks, some judicious debugging through the camel code revealed that I did not have the required jar for unmarshalling the JSON. Adding compile group: 'org.apache.camel', name: 'camel-jackson', version: '2.21.1' to my gradle fixed the issue.


On Monday, May 28, 2018 6:42 PM, Steve Hiller <shetc@xxxxxxxxxxxxx> wrote:



Hi All,


I am in the process of converting from the Spring XML style Camel configuration to using the Java DSL style, as this seems to be the preferred approach when using Spring Boot. I am trying to create a simple REST POST using org.apache.camel.builder.RouteBuilder but I keep getting the following exception:


org.apache.camel.processor.binding.BindingException: Cannot bind to json as message body is not json compatible. Exchange[ID-Mac-the-Knife-local-1527476025699-0-1]

  at org.apache.camel.processor.RestBindingAdvice.unmarshal(RestBindingAdvice.java:248) ~[camel-core-2.21.1.jar!/:2.21.1]

  at org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:128) ~[camel-core-2.21.1.jar!/:2.21.1]

  at org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:48) ~[camel-core-2.21.1.jar!/:2.21.1]

  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:149) ~[camel-core-2.21.1.jar!/:2.21.1]

  at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) [camel-core-2.21.1.jar!/:2.21.1]

  at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:208) [camel-http-common-2.21.1.jar!/:2.21.1]

  at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:78) [camel-http-common-2.21.1.jar!/:2.21.1]


for the request:


curl -X POST \

http://localhost:8080/api/candidate/applyx \

-H 'Cache-Control: no-cache' \

-H 'Content-Type: application/json' \

-d '{

"firstName": "Steve",

"lastname": "Hiller"

}'


Here is the code:


@Component

public class RestRoute extends RouteBuilder {


  @Override

  public void configure() 

  throws Exception 

  {

    restConfiguration()

      .component("servlet")

      .bindingMode(RestBindingMode.json);


    rest("/candidate")

     .post("applyx")

       .consumes(MediaType.APPLICATION_JSON.getType())

       .type(Candidate.class)

       .to("bean:candidateService?method=passwordlessApply");

    }

}


public interface CandidateService {

  void passwordlessApply(Candidate candidate);

}



@Component("candidateService")

public class CandidateServiceImpl implements CandidateService {

  @Override

  public void passwordlessApply(Candidate candidate) {

    System.out.println("Hello World!");

  }

}


public class Candidate implements Serializable {


  private static final long serialVersionUID = 1L;


  private String id;

  private String firstName;

  private String lastName;


  public Candidate() {}


  public String getId() {

    return id;

  }


  public void setId(String id) {

    this.id = id;

  }


  public String getFirstName() {

    return firstName;

  }


  public void setFirstName(String firstName) {

    this.firstName = firstName;

  }


  public String getLastName() {

    return lastName;

  }


  public void setLastName(String lastName) {

    this.lastName = lastName;

  }

}


Seems straightforward enough -- any ideas what I am doing wrong?


Thanks,


Steve