title: How to Call a RESTful web service that have Spring Secuirty with POST parameters from an Android App
body: <p>I have a Spring MVC web application protected with Spring Secuirty and it's configured using Java annotations, this is My Spring MVC application security config classes</p>
  
  <pre><code>package seguridad;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
  
  
  
  @Configuration
  @EnableWebSecurity
  //@EnableWebMvcSecurity
  
  public class SeguridadConfiguracion extends WebSecurityConfigurerAdapter {
  
      @Autowired 
      private AutenticarProvider autenticador;
  
      @Override
      protected void configure( HttpSecurity http ) throws Exception 
      {
          http
              .authenticationProvider(autenticador)
              .authorizeRequests()
                  .antMatchers("/resources/**").permitAll()
                  .antMatchers("/css/**").permitAll() 
                  .antMatchers("/js/**").permitAll()
                  .antMatchers("/img/**").permitAll() 
                  .antMatchers("/sound/**").permitAll() 
                  .antMatchers("/fonts/**").permitAll()
                  .antMatchers("/ajax/**").permitAll()
                  .antMatchers("/php/**").permitAll()
                  .antMatchers("/xml/**").permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                      .loginPage("/login/login")
                      .permitAll()
                      .and()
                  .logout()                                    
                      .permitAll()
                      .logoutSuccessUrl("/login/login?logout")
                  .and()
                      .csrf().disable();
      }
  
  }
  </code></pre>
  
  <p>My AuthProvider</p>
  
  <pre><code>@Component
  public class AutenticarProvider implements AuthenticationProvider {
  
      @Override
      public Authentication authenticate(Authentication authentication)
              throws AuthenticationException {
  
          String name = null;
          String password = null;
          Authentication auth = null;
          boolean pruebaLDAP = false;
  
          try {
              name = authentication.getName();
              password = authentication.getCredentials().toString();
  
  
              if (name.equals("user") &amp;&amp; password.equals("MyPassword")) 
              {
                  List&lt;GrantedAuthority&gt; grantedAuths = new ArrayList&lt;&gt;();
                  grantedAuths.add(new SimpleGrantedAuthority("PERMISO_PRIMERO"));
                  grantedAuths.add(new SimpleGrantedAuthority("PERMISO_MODIFICAR_PERMISO"));
                  grantedAuths.add(new SimpleGrantedAuthority("PERMISO_CONSULTAR_PERMISO"));
  
                  auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
              }
  
              } catch (AuthenticationException e) {
                  e.printStackTrace();
                  throw e;
              }
  
              return auth;
          }
  
          @Override
          public boolean supports(Class&lt;?&gt; authentication) {
              return authentication.equals(UsernamePasswordAuthenticationToken.class);
          }
  </code></pre>
  
  <p>This is my Controller class of the Web service Restful method that I want to call, The method receive a JSON string then I retreive the values I want to from the JSON string and then assign those values to an POJO Object then return the Object</p>
  
  <pre><code>@Controller
  @RequestMapping("/jsp/")
  public class InicioControlador {
  
  
  
      @RequestMapping(value = "changeNameAfter", method = RequestMethod.POST)
      public @ResponseBody
      Person methodChangeStringPro(@RequestBody final String json, Model model)
              throws IOException {
  
          ObjectMapper mapper = new ObjectMapper();
  
          Map&lt;String, String&gt; StringValues;
          StringValues = mapper.readValue(json, HashMap.class);
  
          String stringId = StringValues.get("id");
          String name = StringValues.get("name");
  
          System.out.println("EL STRING ID vale " + stringId);
          System.out.println("EL name  vale " + name);
  
          int realId = Integer.parseInt(stringId);
  
          String newName = name + "KEK";
          realId = realId + 1;
  
          Person person = new Person();
          person.setName(newName);
          person.setId(realId);
  
  
          return person;
      }
  }
  </code></pre>
  
  <p>And this is the code from My android app where I'm trying to call that method</p>
  
  <p>My Main Activity class</p>
  
  <pre><code>public class MainActivity extends AbstractAsyncActivity {
  
  protected static final String TAG = MainActivity.class.getSimpleName();
  
  // ***************************************
  // Activity methods
  // ***************************************
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Initiate the request to the protected service
      final Button submitButton = (Button) findViewById(R.id.submit);
      submitButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              new FetchSecuredResourceTask().execute();
          }
      });
  }
  
  // ***************************************
  // Private methods
  // ***************************************
  private void displayResponse(Person response) {
      Toast.makeText(this, response.getName(), Toast.LENGTH_LONG).show();
  }
  
  // ***************************************
  // Private classes
  // ***************************************
  private class FetchSecuredResourceTask extends AsyncTask&lt;Void, Void, Person&gt; {
  
      private String username;
  
      private String password;
  
      @Override
      protected void onPreExecute() {
          showLoadingProgressDialog();
  
          // build the message object
          EditText editText = (EditText) findViewById(R.id.username);
          this.username = editText.getText().toString();
  
          editText = (EditText) findViewById(R.id.password);
          this.password = editText.getText().toString();
      }
  
      @Override
      protected Person doInBackground(Void... params) {
          final String url = "http://10.0.2.2:8080/ProyectoServidor/jsp/changeNameAfter";
  
          // Populate the HTTP Basic Authentitcation header with the username
          // and password
          HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setAuthorization(authHeader);
          requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
  
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate();
          restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
  
          //Set OBJECT to send
          Person personToSend = new Person();
          personToSend.setId(23);
          personToSend.setName("Prueba");
  
  
          HttpEntity entityPerson = new HttpEntity(personToSend, requestHeaders);
  
          try {
              // Make the network request
              Log.d(TAG, url);
              ResponseEntity&lt;Person&gt; response = restTemplate.exchange(url, HttpMethod.POST,
                      entityPerson, Person.class);
              return response.getBody();
          } catch (HttpClientErrorException e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return new Person();
          } catch (ResourceAccessException e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return new Person();
          }
      }
  
      @Override
      protected void onPostExecute(Person result) {
          dismissProgressDialog();
          displayResponse(result);
      }
  
  }
  </code></pre>
  
  <p>My AbstractAsyncActivity class</p>
  
  <pre><code>public class AbstractAsyncActivity extends Activity {
  
  protected static final String TAG = AbstractAsyncActivity.class.getSimpleName();
  
  private ProgressDialog progressDialog;
  
  private boolean destroyed = false;
  
  // ***************************************
  // Activity methods
  // ***************************************
  @Override
  protected void onDestroy() {
      super.onDestroy();
      destroyed = true;
  }
  
  // ***************************************
  // Public methods
  // ***************************************
  public void showLoadingProgressDialog() {
      this.showProgressDialog("Loading. Please wait...");
  }
  
  public void showProgressDialog(CharSequence message) {
      if (progressDialog == null) {
          progressDialog = new ProgressDialog(this);
          progressDialog.setIndeterminate(true);
      }
  
      progressDialog.setMessage(message);
      progressDialog.show();
  }
  
  public void dismissProgressDialog() {
      if (progressDialog != null &amp;&amp; !destroyed) {
          progressDialog.dismiss();
      }
  }
  }
  </code></pre>
  
  <p>I think that the problem are in this line from my Main Acitivity class</p>
  
  <pre><code>// Populate the HTTP Basic Authentitcation header with the username
          // and password
          HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setAuthorization(authHeader);
          requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
  
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate();
          restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
  
          //Set OBJECT to send
          Person personToSend = new Person();
          personToSend.setId(23);
          personToSend.setName("Prueba");
  
          HttpEntity entityPerson = new HttpEntity(personToSend, requestHeaders);
  
          try {
              // Make the network request
              Log.d(TAG, url);
              ResponseEntity&lt;Person&gt; response = restTemplate.exchange(url, HttpMethod.POST,
                      entityPerson, Person.class);
              return response.getBody();
          } catch (HttpClientErrorException e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return new Person();
          } catch (ResourceAccessException e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
              return new Person();
          }
  </code></pre>
  
  <p>Since the ERROR that I'm getting in my LogCat is </p>
  
  <pre><code>10-16 00:33:51.054: E/AndroidRuntime(1481): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [modelo.Person] and content type [text/html;charset=ISO-8859-1]
  </code></pre>
  
  <p>I was following this tutorial but I adapted it for my needs 
  <a href="https://github.com/spring-projects/spring-android-samples/tree/master/spring-android-basic-auth" rel="nofollow">https://github.com/spring-projects/spring-android-samples/tree/master/spring-android-basic-auth</a></p>
  
  <p>I'm using the following libs</p>
  
  <pre><code>jackson-core-asl-1.9.11.jar
  jackson-mapper-asl-1.9.13.jar
  spring-android-core-1.0.0.RELEASE.jar
  spring-android-rest-template-1.0.1.RELEASE.jar
  </code></pre>
  
  <p><strong>EDIT</strong>
  User @ravindra the classNotFoundError was because I used <code>jackson-core-asl-1.9.11.jar</code> instead of <code>jackson-core-2.6.1.jar</code> after I added this .JAR and this one <code>jackson-annotations-2.6.1.jar</code> the class not found exception was solved. But I still have the original error <code>10-16 04:44:37.524: E/AndroidRuntime(1349): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [modelo.Person] and content type [text/html;charset=ISO-8859-1]
  </code> that made me ask this question.</p>
  
  <p>This are the changes that I did and when I think is the problem </p>
  
  <pre><code>// Populate the HTTP Basic Authentitcation header with the username
          // and password
          HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setAuthorization(authHeader);
          requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
  
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate();
          restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  
          // Set OBJECT to send
          Person personToSend = new Person();
          personToSend.setId(23);
          personToSend.setName("Prueba");
  
          HttpEntity&lt;Person&gt; entityPerson = new HttpEntity&lt;Person&gt;(personToSend, requestHeaders);
  
          try {
              String json = "";
  
              JSONObject jsonObject = new JSONObject();
  
              jsonObject.accumulate("id", "2");
              jsonObject.accumulate("name", "hola");
              json = jsonObject.toString();
  
              // Make the network request
              Log.d(TAG, url);
  
              ResponseEntity&lt;Person&gt; response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity&lt;Object&gt;(requestHeaders), Person.class);
              return response.getBody();
  </code></pre>
  
  <p>I tried to use <code>MappingJackson2HttpMessageConverter</code> as my converter as is suggested here in this similar questions: </p>
  
  <p><a href="https://stackoverflow.com/questions/13038529/force-spring-resttemplate-to-use-xmlconverter">Force Spring RestTemplate to use XmlConverter</a> and <a href="https://stackoverflow.com/questions/24723394/could-not-extract-response-no-suitable-httpmessageconverter-found-for-response">Could not extract response: no suitable HttpMessageConverter found for response type</a>, but still I get the same error</p>
  
  <p>but still I get the same error `10-16 04:44:37.524: E/AndroidRuntime(1349): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [modelo.Person] and content type [text/html;charset=ISO-8859-1]</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I changed my controller annotations and added the <code>produces = "application/json"</code> into the RequestMappin method like this </p>
  
  <pre><code>@RequestMapping(value = "changeNameAfter", method = RequestMethod.POST, produces = "application/json")
      public @ResponseBody
      Person methodChangeStringPro(@RequestBody final String json, Model model)
              throws IOException {
  </code></pre>
  
  <p>But still it gives me the same error. </p>
  
  <pre><code>E/AndroidRuntime(1349): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [modelo.Person] and content type [text/html;charset=ISO-8859-1]
  </code></pre>