cancel
Showing results for 
Search instead for 
Did you mean: 

CAP Java: 404 error when Mocking Remote Service as OData Service

MioYasutake
Active Contributor
0 Kudos
654

Hi community,

I am currently trying to call a remote OData service from a CAP Java application, following the documentation below.

https://cap.cloud.sap/docs/guides/using-services#mock-remote-service-as-odata-service-java

I am attempting to run it locally using the method "Mock Remote Service as OData Service," but I am encountering a 404 error. 

I run the application with the following command:

 

mvn spring-boot:run -Dspring-boot.run.profiles=default,mocked

 

When I send a request, I get the following error:

 

The remote OData service responded with status code '404' (service 'CatalogService', event 'READ', entity 'CatalogService.Books')

 

Additionally, when I run the service with the mvn spring-boot:run command, I am able to retrieve mock data. If you have any insight into the cause of the error, please let me know.

The code is located in the "mock-remote-service" branch of the following GitHub repository.

https://github.com/miyasuta/cap-java-remote-consumer/tree/mock_remote_service


Implementation details:

Service definition:

 

using { CatalogService as external } from './external/CatalogService';

service BooksService {
    @readonly
    entity Books as projection on external.Books;
}

 

Event handler implementation:

 

@Component
@ServiceName(BooksService_.CDS_NAME)
public class BooksServiceHandler implements EventHandler{
    @Autowired
    @Qualifier(CatalogService_.CDS_NAME)
    CqnService catalog;

    Logger logger = LoggerFactory.getLogger(BooksServiceHandler.class);

    @On(event = CqnService.EVENT_READ, entity = Books_.CDS_NAME)
    public Result readBooks(CdsReadEventContext context) {
        logger.info("read handler called");
        return catalog.run(context.getCqn());
    }
}

 

application.yaml configuration:

 

spring:
  config.activate.on-profile: mocked
cds:
  application.services:
    CatalogService-mocked:
      model: CatalogService
      serve:
        path: CatalogService
  remote.services:
    CatalogService:
      destination:
        name: "catalogservice-mocked"    

 

I implemented the DestinationConfiguration.java based on the following Git repository.

https://github.com/SAP-samples/cloud-cap-samples-java/blob/b14e4f9b51a350984f903c9f746f9cb888dd7df2/...

 

abhinavsingh04
Explorer
0 Kudos

Hi @MioYasutake,

I mocked Remote Service as OData Service and it is working properly.
But, after deploying it, i am not able to call the remote service.

application.yaml, i am using application defined Application Defined Destinations in Java

spring:
  config.activate.on-profile: cloud
  # sql.init.schema-locations: classpath:schema-nomocks.sql
cds:
  remote.services:
    CatalogService:
      type: "odata-v4"
      destination:
        properties:
          url: https://81fff548trial-dev-cap-target-service.cfapps.us10-001.hana.ondemand.com
          suffix: "/odata/v4"
          authentication: TokenForwarding

Response i am getting on the browser,

{
"error": {
"code": "50012005",
"message": "The remote OData service responded with status code '200'"
}
}

these are the logs, i fetched from Application Logging Service,

Aug 13, 2024 @ 05:18:52.774
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
com.sap.cds.services.impl.ServiceImpl
Finished emit of 'ApplicationLifecycleService$Default' for event 'ERROR_RESPONSE', entity ''

Aug 13, 2024 @ 05:18:52.769
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
com.sap.cds.services.impl.ServiceImpl
Executing ON handler 'public void com.sap.cds.services.impl.application.ErrorResponseHandler.onErrorResponse(com.sap.cds.services.application.ErrorResponseEventContext)' (order 11000) for event 'ERROR_RESPONSE' on service 'ApplicationLifecycleService$Default' and entity ''

Aug 13, 2024 @ 05:18:52.769
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
com.sap.cds.services.impl.ServiceImpl
Started emit of 'ApplicationLifecycleService$Default' for event 'ERROR_RESPONSE', entity ''

Aug 13, 2024 @ 05:18:52.744
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
ERROR
com.sap.cds.adapter.odata.v4.processors.CdsProcessor
The remote OData service responded with status code '200' (service 'CatalogService', event 'READ', entity 'CatalogService.Books')

Aug 13, 2024 @ 05:18:52.744
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
INFO
com.sap.cds.adapter.odata.v4.processors.CdsProcessor
Exception marked the ChangeSet 3 as cancelled: The remote OData service responded with status code '200' (service 'CatalogService', event 'READ', entity 'CatalogService.Books')

Aug 13, 2024 @ 05:18:52.742
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG



com.sap.cds.services.impl.odata.RemoteODataClient
Received OData response with status code '200' and error message '<html><head><link rel="shortcut icon" href="data&colon;image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><script>document.cookie="fragmentAfterLogin="+encodeURIComponent(location.hash)+";path=/;";document.cookie="locationAfterLogin="+encodeURIComponent(location.href.split('#')[0].split(location.host)[1])+";path=/;";document.cookie="signature=IsXmcTjzO5SxaN3mzg04rlKJ%2BDI%3D;path=/;";location="https://81fff548trial.authentication.us10.hana.ondemand.com/oauth/authorize?response_type=code&client_id=sb-cap-target-service-81fff548trial-dev!t294135&redirect_uri=https%3A%2F%2F81fff548trial-dev-cap-target-service.cfapps.us10-001.hana.ondemand.com%2Flogin%2Fcallback"</script></head></html>'

Aug 13, 2024 @ 05:18:52.738
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
WARN
com.sap.cloud.sdk.datamodel.odata.client.ODataResponseDeserializer
JSON path JsonPath(nodes=[value]) could not be resolved for "<html><head><link" at position 0.

Aug 13, 2024 @ 05:18:52.725
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
org.apache.http.impl.conn.DefaultManagedHttpClientConnection
http-outgoing-0: set socket timeout to 0

Aug 13, 2024 @ 05:18:52.725
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
Connection released: [id: 0][route: {s}->https://81fff548trial-dev-cap-target-service.cfapps.us10-001.hana.ondemand.com:443][total available: 1; route allocated: 1 of 100; total allocated: 1 of 200]

Aug 13, 2024 @ 05:18:52.724
cap-remote-service-srv
1fc91461-f5b5-49ae-6525-7e36959627a4
DEBUG
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
Connection [id: 0][route: {s}->https://81fff548trial-dev-cap-target-service.cfapps.us10-001.hana.ondemand.com:443] can be kept alive indefinitely

 Please let me know, how to resolve this issue.

Thanks,

Abhinav

Accepted Solutions (1)

Accepted Solutions (1)

Dinu
Contributor

Services including mocks are now hosted with prefix "/odata/v4". So you need to add this to the url for the destination or as a suffix for the "remote.service".

spring:
  config.activate.on-profile: mocked
cds:
  application.services:
    CatalogService-mocked:
      model: CatalogService
      serve:
        path: CatalogService
  remote.services:
    CatalogService:
      destination:
        name: "catalogservice-mocked"  
        suffix: "/odata/v4"  
MioYasutake
Active Contributor
0 Kudos
Thank you for your prompt reply! It worked!

Answers (0)