Home > Software engineering >  How to retreive Id from the API
How to retreive Id from the API

Time:09-21

I am using java spring. So inside the database, I have the content table which have page Id column and content column. Inside this content column, there are many tracker Ids, saved as ContentJSON. I am trying to do the GET API using this url: localhost:8080/eHealth/api/user/mytracker/pageId?trackerId=2. However, it is showing the list of trackerIds under that specific pageIds.

My code is as follow: //GET each tracker data

@SuppressWarnings("rawtypes")
    @GetMapping(value = "/mytracker/{pageId}/{trackerId}")
    public ResponseEntity<?> getTrackerContent(HttpServletRequest request, 
            @PathVariable String pageId, @PathVariable String trackerId) throws JsonParseException, JsonMappingException, IOException {
        ValidationResult validationResult = this.tokenValidator.validateUserIdentityFromRequest(request);
        if(validationResult != ValidationResult.VALID_USER) {
            return this.tokenValidator.httpResponseEntityWithTemplateMessages(validationResult);
        }
        User currentUser = this.tokenValidator.getUserFromToken(this.tokenValidator.resolveToken(request));
        UserContent trackerContent = this.portalService.getUserContent(currentUser, pageId, "tracker");
        HashMap unpackedTrackerContent = this.unpackTrackerData(trackerContent);
        HashMap<String, Object> trackers = new HashMap<String, Object>();
        trackers.put(pageId,unpackedTrackerContent);
        return ResponseEntity.ok(trackers);     
    }

Any help would be appreciated. Thank you

The content column inside the database is as folows:

{"content":null,"UserContentJSON":"{\"trackerData\":[{\"trackerId\":\"1\",\"trackerDateTime\":\"05-07-2020T03:47:00.00Z\",\"trackerIntensity\":\"2\",\"trackerDuration\":\"1\"},{\"trackerId\":\"2\",\"trackerDateTime\":\"05-07-2020T03:47:00.00Z\",\"trackerIntensity\":\"2\",\"trackerDuration\":\"1\"},{\"trackerId\":\"3\",\"trackerDateTime\":\"05-07-2020T03:47:00.00Z\",\"trackerIntensity\":\"2\",\"trackerDuration\":\"1\"}]}","isNew":false,"ref":"tracker","programID":"HOPE_CVD_Copy","pageID":"physicalactivity","userID":"cvdtest"}

CodePudding user response:

"I am trying to do the GET API using this url: localhost:8080/eHealth/api/user/mytracker/pageId?trackerId=2. However, it is showing the list of trackerIds under that specific pageIds"

The controller expects 2 @PathVariable and not a @RequestParam

@GetMapping(value = "/mytracker/{pageId}/{trackerId}")
public ResponseEntity<?> getTrackerContent(HttpServletRequest request, 
            @PathVariable String pageId, @PathVariable String trackerId) throws...{
        

So you should make a request like: localhost:8080/eHealth/api/user/mytracker/3/2 where 3 is the 'pageId' and 2 is the 'trackerId'

CodePudding user response:

Like @Dirk said you made mistake in your URL.

Your URL should be something like this,

localhost:8080/eHealth/api/user/mytracker/{yourPageId}/{yourTrackerId}

That being said, in you implementation I don't see that variable @PathVariable String trackerId is used anywhere. You may need to check on that.

  • Related