set destinations=[{name: "Erp1809", url: "http://<host>:<port>", username: "xxx", password: "xxx", sap-language: "ja"}]
package com.sap.cloud.sdk;
import com.google.gson.Gson;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.odatav2.connectivity.ODataException;
import com.sap.cloud.sdk.s4hana.connectivity.DefaultErpHttpDestination;
import com.sap.cloud.sdk.s4hana.connectivity.ErpHttpDestination;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.Customer;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultBusinessPartnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/customer")
public class GetCustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(GetCustomerServlet.class);
private static final ErpHttpDestination destination =
DestinationAccessor.getDestination("Erp1809")
.asHttp().decorate(DefaultErpHttpDestination::new);
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws IOException {
logger.info("Start get method: " + request.getRequestURI());
String parameter = request.getParameter("name");
logger.info("Get parameter 'name': " + parameter);
try{
final String searchParameter = "substringof(CustomerName, '" + parameter + "')";
final List<Customer> customers =
new DefaultBusinessPartnerService()
.getAllCustomer()
.withQueryParameter("$filter", searchParameter)
.select(Customer.CUSTOMER, Customer.CUSTOMER_FULL_NAME)
.top(5)
.execute(destination);
logger.info(new Gson().toJson(customers));
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8"); //日本語用
response.getWriter().write(new Gson().toJson(customers));
} catch (final ODataException e){
logger.error(e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(e.getMessage());
}
}
}
# from directory application
mvn clean package && mvn tomee:run
[{"Customer":"9","CustomerFullName":" test/ test"},{"Customer":"223","CustomerFullName":" HT Test/1234567 TOKYO"},{"Customer":"10000","CustomerFullName":" TEST/111-1111 TEST"},{"Customer":"10001","CustomerFullName":" TEST/111-1111 TEST"},{"Customer":"99993","CustomerFullName":" test/ tokyo"}]
09:56:37.566 [http-nio-8080-exec-2] INFO com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor - Creating a new DestinationLoaderChain with EnvVarDestinationLoader as the primary DestinationLoader implementation.
09:56:38.797 [http-nio-8080-exec-2] INFO com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor - Using an instance of ScpCfDestinationLoader as the secondary DestinationLoader implementation.
09:56:40.662 [http-nio-8080-exec-2] INFO com.sap.cloud.sdk.GetCustomerServlet - Start get method: /customer
09:56:40.665 [http-nio-8080-exec-2] INFO com.sap.cloud.sdk.GetCustomerServlet - Get parameter 'name': test
09:56:43.857 [http-nio-8080-exec-2] INFO com.sap.cloud.sdk.GetCustomerServlet - [{"Customer":"9","CustomerFullName":" test/ test"},{"Customer":"223","CustomerFullName":" HT Test/1234567 TOKYO"},{"Customer":"10000","CustomerFullName":" TEST/111-1111 TEST"},{"Customer":"10001","CustomerFullName":" TEST/111-1111 TEST"},{"Customer":"99993","CustomerFullName":" test/ tokyo"}]
cf login
cf create-service destination lite my-destination
---
applications:
- name: cloud-sdk-test
memory: 1024M
timeout: 300
routes:
- route: https://<rounte>.cfapps.eu10.hana.ondemand.com
random-route: false
path: application/target/cloud-sdk-test-application.war
buildpacks:
- sap_java_buildpack
env:
TARGET_RUNTIME: tomee7
SET_LOGGING_LEVEL: '{ROOT: INFO, com.sap.cloud.sdk: INFO}'
JBP_CONFIG_SAPJVM_MEMORY_SIZES: 'metaspace:128m..'
services:
# - my-application-logs
# - my-xsuaa
- my-destination
# - my-connectivity
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
10 | |
10 | |
7 | |
7 | |
7 | |
6 | |
6 | |
5 | |
4 |