Apache Camel 中的类型转换错误
TypeConversion Errors in Apache Camel
我正在对 Apache Camel 作为请求聚合器是否符合我的要求进行 POC。
假设我有一个现有系统,我在其中创建了两种不同服务的用户。因此,对于 Camel,我希望将这两个步骤聚合到一个聚合请求中。
我在这里有两个不同的问题。
- 聚合器模式是执行此操作的正确方法吗?意思是,最后,如果我对第一个服务的调用因任何原因失败,我希望我的整个请求失败。
- 其次,[请理解我仍然是 Camel 的新手并纠正我] 我尝试实现一个基本流程,我想在其中进行一些转换,我想从 DemoUserRequest 的用户名中提取名字,如下所示将其传递给 DemoUserMS1Request,然后调用 MS2。
@Component
public class DemoRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.component("servlet")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true");
rest("/api/v1")
.description("Create user flow")
.id("create-user")
.consumes(MediaType.APPLICATION_JSON_VALUE)
.produces(MediaType.APPLICATION_JSON_VALUE)
.post("/user")
.to("direct:createUser");
from("direct:createUser")
.log(" :: ${body} ")
.marshal().json(JsonLibrary.Jackson)
.unmarshal().json(JsonLibrary.Jackson, DemoUserRequest.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
DemoUserRequest demoUserRequest = exchange.getIn(DemoUserRequest.class);
log.info("DemoUserRequest toString : " + demoUserRequest.toString());
DemoUserMS1Request demoUserMS1Request = new DemoUserMS1Request(demoUserRequest.getUserID(),
demoUserRequest.getUserName(),
demoUserRequest.getUserLanguage(),
demoUserRequest.getUserName()
.substring(0, demoUserRequest.getUserName().indexOf(' ')));
message.setBody(demoUserMS1Request);
}
})
.marshal().json(JsonLibrary.Jackson, DemoUserMS1Request.class)
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
// .toD("{{ms1.url}}/api/createUser");
.to("stream:out");
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class DemoUserRequest {
private String userID;
private String userName;
private String userLanguage;
}
@Data
@AllArgsConstructor
public class DemoUserMS1Request {
private String userID;
private String userName;
private String userLanguage;
private String userFirstName;
}
@Data
public class DemoUserMS2Request {
private String userName;
}
我在请求时遇到问题
curl --location --request POST 'http://localhost:8081/api/v1/user' \
--header 'Content-Type: application/json' \
--data-raw '{
"userID" : "1",
"userName" : "abc def",
"userLanguage" : "EN"
}'
Camel 抛出类似
的错误
java.lang.NullPointerException
at com.unbxd.hagrid.routes.DemoRoute.process(DemoRoute.java:42)
[在 log.info 行]
有什么方法可以将我的 JSON 请求投射到我的 POJO 中吗?
你在哪里:
DemoUserRequest demoUserRequest = exchange.getIn(DemoUserRequest.class);
你应该改为:
DemoUserRequest demoUserRequest = exchange.getIn().getBody(DemoUserRequest.class);
我正在对 Apache Camel 作为请求聚合器是否符合我的要求进行 POC。
假设我有一个现有系统,我在其中创建了两种不同服务的用户。因此,对于 Camel,我希望将这两个步骤聚合到一个聚合请求中。 我在这里有两个不同的问题。
- 聚合器模式是执行此操作的正确方法吗?意思是,最后,如果我对第一个服务的调用因任何原因失败,我希望我的整个请求失败。
- 其次,[请理解我仍然是 Camel 的新手并纠正我] 我尝试实现一个基本流程,我想在其中进行一些转换,我想从 DemoUserRequest 的用户名中提取名字,如下所示将其传递给 DemoUserMS1Request,然后调用 MS2。
@Component
public class DemoRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.component("servlet")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true");
rest("/api/v1")
.description("Create user flow")
.id("create-user")
.consumes(MediaType.APPLICATION_JSON_VALUE)
.produces(MediaType.APPLICATION_JSON_VALUE)
.post("/user")
.to("direct:createUser");
from("direct:createUser")
.log(" :: ${body} ")
.marshal().json(JsonLibrary.Jackson)
.unmarshal().json(JsonLibrary.Jackson, DemoUserRequest.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
DemoUserRequest demoUserRequest = exchange.getIn(DemoUserRequest.class);
log.info("DemoUserRequest toString : " + demoUserRequest.toString());
DemoUserMS1Request demoUserMS1Request = new DemoUserMS1Request(demoUserRequest.getUserID(),
demoUserRequest.getUserName(),
demoUserRequest.getUserLanguage(),
demoUserRequest.getUserName()
.substring(0, demoUserRequest.getUserName().indexOf(' ')));
message.setBody(demoUserMS1Request);
}
})
.marshal().json(JsonLibrary.Jackson, DemoUserMS1Request.class)
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
// .toD("{{ms1.url}}/api/createUser");
.to("stream:out");
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class DemoUserRequest {
private String userID;
private String userName;
private String userLanguage;
}
@Data
@AllArgsConstructor
public class DemoUserMS1Request {
private String userID;
private String userName;
private String userLanguage;
private String userFirstName;
}
@Data
public class DemoUserMS2Request {
private String userName;
}
我在请求时遇到问题
curl --location --request POST 'http://localhost:8081/api/v1/user' \
--header 'Content-Type: application/json' \
--data-raw '{
"userID" : "1",
"userName" : "abc def",
"userLanguage" : "EN"
}'
Camel 抛出类似
的错误java.lang.NullPointerException
at com.unbxd.hagrid.routes.DemoRoute.process(DemoRoute.java:42)
[在 log.info 行] 有什么方法可以将我的 JSON 请求投射到我的 POJO 中吗?
你在哪里:
DemoUserRequest demoUserRequest = exchange.getIn(DemoUserRequest.class);
你应该改为:
DemoUserRequest demoUserRequest = exchange.getIn().getBody(DemoUserRequest.class);