Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Trying to pull in JSON from URL and return to Component to loop through

SocialTaylor
Level 3
Level 3

Hello!

 

I've been working through an issue on our site and unsure what I am doing wrong in my Java. What we need to do is return the JSON from this url: https://movement-stage2.adobemsbasic.com/content/dam/mm/internal-marketing-corporate/web/lo-websites... and then loop through those to display within a component. I have created a Service to grab those and then brought in my service to retrieve the data, but I seemingly get a Resource error when trying to test it? Here is my service and screenshots of the models and the htl. Please let me know if you can help me identify this issue!!

 

ServiceImpl:

package com.movementmortgage.core.services.impl;

import com.movementmortgage.core.services.LoanOfficerService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.osgi.service.component.annotations.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;

@Component(service = LoanOfficerService.class, immediate = true)
public class LoanOfficerServiceImpl implements LoanOfficerService {

    private static String readUrl(String urlString) throws Exception {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read); 
    
            return buffer.toString();
        } finally {
            if (reader != null)
                reader.close();
        }
    }

    public static class Info {
        String lotitle;
        String email_address;
        String loan_officer_image;
        String office_phone_number;
        String cell_phone_number;
        String apply_online_url_blend;
        String first_name;
        String last_name;
        String branch_name;
        String branch_address;
        String branch_city;
        String branch_state;
        String branch_zip;
        String crm_enabled;
        String lo_website;
    }

    public static class Officer {
        Integer id;
        String slug;
        String type;
        String link;
        List<Info> acf;
    }

    // public static void main(String[] args) throws Exception {
    //     String json = readUrl("https://movement-stage2.adobemsbasic.com/content/dam/mm/internal-marketing-corporate/web/lo-websites/shared-assets/json/profiles.json");

    //     Gson gson = new Gson();
    //     Officer officer = gson.fromJson(json, Officer.class);
    // }

    @Override
    public List getLoanOfficers() {
        String json;
        
        try {
            json = readUrl("https://movement-stage2.adobemsbasic.com/content/dam/mm/internal-marketing-corporate/web/lo-websites/shared-assets/json/profiles.json");

            ObjectMapper mapper = new ObjectMapper();
            List<Officer> officers = mapper.readValue(json, new TypeReference<List<Officer>>(){});
    
            return officers;
        } catch (Exception e) {
            return null;
        }
    }
}

Screen Shot 2021-09-08 at 3.11.31 PM.png

Screen Shot 2021-09-08 at 3.03.49 PM.pngScreen Shot 2021-09-08 at 3.03.35 PM.pngScreen Shot 2021-09-08 at 3.09.57 PM.pngScreen Shot 2021-09-08 at 3.08.21 PM.png

1 Accepted Solution
fanindras
Correct answer by
Level 5
Level 5

Hi @SocialTaylor - Glad that we figured out that the issue is with bundle.

 

If your version of bundle is not updated, then check if the code package is successfully installed in crx/packmgr. If yes, then try to re-install the package and look for any possible errors in the logs.

Additionally, you can deploy the bundle alone manually via /system/console/bundles page.

 

On the versioning

===================

For local development, you can always have -SNAPSHOT version, and AEM will override the -SNAPSHOT jar each time you deploy.

For deployment to higher environments - If you are using Cloud Manager, it will take care of creating a valid version number for each build based on timestamp as per below screenshot and you don't need to manage the version in pom.

Screenshot 2021-09-09 at 12.13.18 PM.png

version in pom.xml - will be generated by Cloud Manger, without any action from your end.

 

If you still want to manage versioning of your builds, you can opt for maven release plugin and below are some useful links.

http://maven.apache.org/maven-release/maven-release-plugin/

https://www.initialyze.com/blog/2020/04/cutting-releases-for-your-aem-projects-using-maven/

https://github.com/Adobe-Marketing-Cloud/aem-sample-we-retail-commons/blob/master/pom.xml

 

Thanks,

Fani

View solution in original post

11 Replies
rush_pawan
Community Advisor
Community Advisor

Hello,

 

The log file describe the error coming from file layoutloanofficers_html.java at line 39

 

Now, to find the file go to /crx-quickstart/launchpad/felix and search for your class file layoutloanofficers_html.java (sightly java file) using below shell command

 

find . -name "layoutloanofficers_html.java"

 

then start debugging from there.

 

hope it will help!!

rush_pawan
Community Advisor
Community Advisor

Hello,

 

its AEM installed folder structure which you should access from your server (not as url) and then run shell or similar command mentioned above.

 

Thanks!!

Kiran_Vedantam
Community Advisor
Community Advisor

Hi @SocialTaylor,

 

In the Model, try @Inject instead of @OSGiService for the services to get injected.

KiranVedantam1992_0-1631130438264.png

 

Hope this helps!

 

Thanks,

Kiran Vedantam.

SocialTaylor
Level 3
Level 3

Tried giving this a go and it seems to still be giving me the same error. Not sure if it has to do with the sling model or the htl at this point.

fanindras
Level 5
Level 5

Hi @SocialTaylor - Can you confirm if your bundle containing the Model is deployed and active?

SocialTaylor
Level 3
Level 3

Hey @fanindras !

 

I believer you are onto the correct issue. When I went to my bundles in the AEM console, I do not see either my service nor my model there, as you can see in my screenshot below.

Screen Shot 2021-09-08 at 9.58.58 PM.pngScreen Shot 2021-09-08 at 9.59.35 PM.png

 

Earlier as I was working on this, it made my update my package-info.java from '1.0' to '1.1.0' for both of those after I removed '-SNAPSHOT' from the version of all of my POM.xml files to start proper versioning.  How do I go about telling it to bump those up from 1.0.0 to 1.1.0? And, was that the correct way to go about versioning in my POM.xml files? I don't know a lot about setting up projects in AEM, but we were told removing the '-SNAPSHOTS' were needed for our builds when publishing to the cloud haha.

 

Thank you so much for your help!!!

Taylor

fanindras
Correct answer by
Level 5
Level 5

Hi @SocialTaylor - Glad that we figured out that the issue is with bundle.

 

If your version of bundle is not updated, then check if the code package is successfully installed in crx/packmgr. If yes, then try to re-install the package and look for any possible errors in the logs.

Additionally, you can deploy the bundle alone manually via /system/console/bundles page.

 

On the versioning

===================

For local development, you can always have -SNAPSHOT version, and AEM will override the -SNAPSHOT jar each time you deploy.

For deployment to higher environments - If you are using Cloud Manager, it will take care of creating a valid version number for each build based on timestamp as per below screenshot and you don't need to manage the version in pom.

Screenshot 2021-09-09 at 12.13.18 PM.png

version in pom.xml - will be generated by Cloud Manger, without any action from your end.

 

If you still want to manage versioning of your builds, you can opt for maven release plugin and below are some useful links.

http://maven.apache.org/maven-release/maven-release-plugin/

https://www.initialyze.com/blog/2020/04/cutting-releases-for-your-aem-projects-using-maven/

https://github.com/Adobe-Marketing-Cloud/aem-sample-we-retail-commons/blob/master/pom.xml

 

Thanks,

Fani

View solution in original post

SocialTaylor
Level 3
Level 3

Hey @fanindras !

 

That did seem to be the issue for my model! The only issue now is I'm still unable to get the JSON using the service I created. Unsure if that is an issue with how I am returning that JSON or not. Any thoughts?

 

Taylor

fanindras
Level 5
Level 5

Hey @SocialTaylor - I would say, we may need to confirm if the json to object mapping in your service layer is happening properly. For that, you can add few loggers after the object mapping and print the values from your pojos.

If this test is as expected, we can get to the Sightly rendering part of the flow.

 

Thanks,

Fani

 

Ravi_Pampana
Community Advisor
Community Advisor

Hi,

 

Can you check whether your model folder is added in pom.xml under 

Sling-Model-Packages ?