简体   繁体   中英

While consuming REST services created in Spring boot in Android is throwing the following error

I have a rest service created in spring boot

@RestController
@RequestMapping("/users")
public class UsersController {

@Autowired
private UserDao uDao;

@RequestMapping("/userDetails")
public List<UsersModel> showUserDetails() {
    List<UsersModel> users = uDao.getUserDetails();
    System.out.println("received");
    return users;
}

@GetMapping("/getName")
public String getUserName() {
    return "Das Lima";
}

 }

When calling in browser like http://localhost:2020/SpringBootDemo/getName it is returning fine and displaying the value "Das Lima". I don't know why its not working in android

But When tried to consume in android in AsyncTask like below

   private class UserAunthetication extends AsyncTask<String, String,   String>  {
      @Override
      protected String doInBackground(String... strings) {

        final String url = "http://myIpAddress:2020/SpringBootDemo  /userDetails";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        Object usersModel = restTemplate.getForObject(url, String.class);

        return usersModel.toString();
    }

    @Override
    protected void onPostExecute(String value) {
        Toast.makeText(LoginPageActivity.this, "Inside post method " +  value, Toast.LENGTH_SHORT).show();
    }
}

Encountered with problem like

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token

Looks like its having an error parsing your json - I would wager the list of users isnt being returned as a valid json string from your showUserDetails method. To test out this just return "das lima" from your /userDetails endpoint as well, and i'd imagine it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM