osdir.com

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

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


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