0

I am working on two projects , 1) micro service(MSA) and 2)SDK

I am sending a GET request from the MSA to SDK where I expect to get a JAVA class as the return type , but instead I am returned just the FQDN of the class as a String

MSA code :

 @SuppressWarnings("unchecked")
    @Override
    public Map<String, Class<?>> getSettingClasses() {
        try {
            Map<String, String> queryParam = Maps.newHashMap();
            queryParam.put("sid", userContext.getServiceId().toString());
            Map<String, Class<?>> map = restClient.get(
                    commonSettingService.getApplicationCode(commonSettingService.getServiceDefId(userContext
                            .getServiceId())), MasterMaintenanceEndpoint.GET_SETTING_CLASSES, Map.class,
                    userContext.getSessionToken(), queryParam);
            return map;
        } catch (Exception e) {
            logger.error("Can not connect to Master Maintenance service", e);
            return null;
        }
    }

SDK code :

@RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Class<?>> getSettingClasses() {
        Map<String, Class<?>> clazz = Maps.newHashMap();
        String serviceDefId = getProviderService().getServiceDefId();
        String serviceId = userContext.getServiceId().toString();
        clazz.put("dtoClass", getProviderService().getDtoClass());
        clazz.put("dtoIndexClass", getProviderService().getIndexDtoClass());
        return clazz;
    }

Service code :

Map<String, Class<?>> masterMaintenanceClassMap = webApiService.getSettingClasses();
        Class<?> dtoClass = masterMaintenanceClassMap.get("dtoClass");
        Class<?> dtoIndexClass = masterMaintenanceClassMap.get("dtoIndexClass");

Within SDK code , getProviderService().getDtoClass() returns a java.lang.Class type but in Service code , masterMaintenanceClassMap.get("dtoClass") evaluates to a String type whose value is actually FQDN of the class.

Does REST restrict returning a Class ? Is there a workaround for this ?

Note : I am unable to do Class.forName(fqdn) in my service code as it does not have dependency to the class

  • Rest API or SOAP web service would always return text String as response. So that it can be invoked by other platforms like C#, python, C++ etc. – Bejond Aug 01 '18 at 09:39

1 Answers1

0

You can do that straight forward in a SOAP base web service call. But in REST you should better use json structure and reconvert the json into the required class object. You can do that very easily by using available APIs.

You can found some good example in the below link:-

Converting JSON data to Java object

Hope this will help you.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17