cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Error 500 in CAP entitySet READ

ZMoe
Explorer
0 Likes
1,460

Hi everyone, I'm new to CAP development. I ran into this problem when building a simple demo in the trial environment.

I have successfully deployed my CAP application to BTP and can access the service metadata.

https://xxxtrial-dev-moe-cap-backend-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/My_SRV/$metadata

1.png

 

 

 

 

 

 

 

 

 

But when I access the entitySet, I get the following error:

https://xxxtrial-dev-moe-cap-backend-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/My_SRV/MoeSet

2.png

 

 

 

I generated the CAP framework using the following command: 

mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds -DarchetypeVersion=RELEASE -DgroupId=com.moe.company -DartifactId=moe-cap-backend -Dpackage=com.moe.company.moecapbackend

 

Then I created capservice.cds under the srv directory with the following content:

service My_SRV {
    @requires: 'any'
    entity MoeSet as projection on TestSet;
}

entity TestSet {
    key testKey : String(64);
    inputData : String(100);
    memo: String(100);
    outputData: String(100);
}

And I also created a MyServiceHandler.java file under the main directory with the following content:

package com.moe.company.mycapbackend;

import java.util.HashMap;
import java.util.Map;
import com.sap.cds.services.cds.CdsReadEventContext;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.request.UserInfo;
import cds.gen.my_srv.MoeSet;


@ServiceName("My_SRV")
public class MyServiceHandler implements EventHandler {

  @On(event = CqnService.EVENT_READ, entity = "My_SRV.MoeSet")
  public void beforeReadProducts(CdsReadEventContext context) {
    UserInfo userInfo = context.getUserInfo();

    MoeSet datamodel = com.sap.cds.Struct.create(MoeSet.class);
    datamodel.put(MoeSet.MEMO, userInfo.getName());

    Map<Object, Map<String, Object>> result = new HashMap<>();
    String key ="test1";
    result.put(key, datamodel);

    context.setResult(result.values());

  }
}

Since this is just a simple practice, I didn't add authentication. Here's my mta.yaml file:

_schema-version: 3.3.0
ID: moe-cap-backend
version: 1.0.0-SNAPSHOT
description: "A simple CAP project."
parameters:
  enable-parallel-deployments: true
modules:
  - name: moe-cap-backend-srv
    type: java
    path: srv
    parameters:
      instances: 1
      buildpack: sap_java_buildpack_jakarta
    properties:
      SPRING_PROFILES_ACTIVE: cloud,sandbox
      JBP_CONFIG_COMPONENTS: "jres: ['com.sap.xs.java.buildpack.jre.SAPMachineJRE']"
      JBP_CONFIG_SAP_MACHINE_JRE: '{ version: 17.+ }'
    build-parameters:
      builder: custom
      commands:
        - mvn clean package -DskipTests=true --batch-mode
      build-result: target/*-exec.jar
    provides:
      - name: srv-api # required by consumers of CAP services (e.g. approuter)
        properties:
          srv-url: ${default-url}

 

Are there any steps I might have missed?

View Entire Topic
MioYasutake
SAP Champion
SAP Champion
0 Likes

@ZMoe 

If you don't use a database, try adding the following profile to the application.yaml.

---
spring:
  config:
    activate:
      on-profile: cloud
cds:
  data-source:
    auto-config:
      enabled: false

 

ZMoe
Explorer
0 Likes

@MioYasutake  Thank you for your reply.

My application.yaml was automatically generated with the same content as yours. So maybe it shouldn't be the issue here.

I later tried some other methods to fix the error mentioned above. Finally, I resolved it using the approach below (at least that error no longer appears, though I don't understand why). But thank you very much anyway.

I modified the structure/writing style of my .cds file.

Before:

service My_SRV {
    entity MoeSet as projection on TestSet;
}

entity TestSet {
    key testKey : String(64);
    inputData : String(100);
    memo: String(100);
    outputData: String(100);
}

 

After:

service My_SRV {
    entity TestSet {
		key testKey : String(64);
		inputData : String(100);
		memo: String(100);
		outputData: String(100);
    }
}