我目前正在开发一个 Spring Boot Camel 项目,我在其中配置了一条路由。但是,当我尝试使用 cURL 或 Postman 访问路由时,我收到“404 Not Found”响应。我需要帮助来解决这个问题。
@Configuration
public class CamelWebConfiguration {
private static final String CAMEL_URL_MAPPING = "/v1/*";
private static final String CAMEL_SERVLET_NAME = "CamelServlet";
private final RoutesConfiguration routesConfiguration;
@Autowired
public CamelWebConfiguration(RoutesConfiguration routesConfiguration) {
this.routesConfiguration = routesConfiguration;
}
@Bean
public ServletRegistrationBean<CamelHttpTransportServlet> servletRegistrationBean() throws Exception {
ServletRegistrationBean<CamelHttpTransportServlet> registration =
new ServletRegistrationBean<>(new CamelHttpTransportServlet(), CAMEL_URL_MAPPING);
registration.setName(CAMEL_SERVLET_NAME);
System.out.println("2222211111111111 " + registration.getUrlMappings());
routesConfiguration.configure();
return registration;
}
}
@Component
public class RoutesConfiguration extends RouteBuilder {
@Override
public void configure() throws Exception {
getContext().setTracing(true); // Enable Camel route tracing
System.out.println("22222222222");
onException(Exception.class)
.handled(true)
.log(LoggingLevel.ERROR, log, "Error processing XML request: ${exception.message}")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500))
.setBody(constant("Error processing XML request"))
.to("direct:errorHandler");
from("direct:errorHandler")
.log("Handling error: ${body}")
// Additional error handling logic goes here
.end();
// Configure REST configuration
restConfiguration()
.enableCORS(true)
.port(5001)
.component("servlet")
.bindingMode(RestBindingMode.auto);
// Define existing REST endpoint
rest("/test/")
.post("/api")
.id("api-route")
.consumes("application/xml; charset=UTF-8")
.produces("application/xml; charset=UTF-8")
.routeId("request-handler")
.type(String.class)
.outType(String.class)
.to("direct:processXml");
// Camel route for processing XML data
from("direct:processXml")
.autoStartup(true)
.routeId("request-handler")
.log(LoggingLevel.INFO, log, "Received XML request: ${body}")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/xml"))
.setExchangePattern(ExchangePattern.InOut)
.convertBodyTo(String.class)
.process(xmlProcessor)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.log(LoggingLevel.INFO, "Processed XML response: ${body}")
.end();
}
@PostConstruct
public void startRoute() throws Exception {
RouteController routeController = camelContext.getRouteController();
routeController.startRoute("request-handler");
}
}
我目前正在开发一个 Spring Boot Camel 项目,我在其中配置了一条路由。但是,当我尝试使用 cURL 或 Postman 访问路由时,我收到“404 Not Found”响应。我需要帮助来解决这个问题。 但是
当我尝试使用邮递员或卷曲的网址时,它有一个响应 404 notfound 骆驼版本 4.3.0 spring boot 3.2.4 java 17 http://localhost:5001/v1/test/api
检查您的代码片段
rest("/test/").post("/api")
会将端点构造为 /test//api
你可以尝试一下
rest("/test").post("/api")
或
rest("/test/").post("api")