title: HTTP Post request with content type application/x-www-form-urlencoded not working in Spring boot service
body: <p>I am new to spring boot recently i am trying to do HTTP POST request from one android application through retrofit2 rest API library with application/x-www-form-url encoded but when i hit my spring boot POST service it shows me below error</p>
  
  <blockquote>
    <p>"status":415,"error":"Unsupported Media
    Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content
    type 'application/x-www-form-urlencoded;charset=UTF-8' not
    supported","path":"/api/login"</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/UCaLL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UCaLL.png" alt="enter image description here"></a></p>
  
  <p><strong>Can anyone know how to solve it?</strong></p>
  
  <p><strong>Here is my code Android Code example</strong> </p>
  
  <p>ApiService.java</p>
  
  <pre><code>public interface ApiService {
      @FormUrlEncoded
      @POST("/api/login")
      Call&lt;LoginData&gt; postLogIn(
              @Field("username") String username,
              @Field("password") String password);
  }
  </code></pre>
  
  <p>ApiHandler.java</p>
  
  <pre><code>    private static final String SERVER_URL = "http://192.168.0.12:8080/";
      private static final long CONNECTION_TIMEOUT = 30;
      public static Retrofit restAdapter;
  
      //public static final int CONNECTION_TIME_OUT = 120;
      private static Retrofit getRestAdapter() {
          if (restAdapter == null) {
              restAdapter = new Retrofit.Builder()
                      .baseUrl(SERVER_URL)
                      .addConverterFactory(GsonConverterFactory.create())
                      .client(getClient()).build();
          }
          return restAdapter;
      }
  
      private static OkHttpClient getClient() {
          OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder();
          HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
          httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
          okClientBuilder.addInterceptor(httpLoggingInterceptor);
          okClientBuilder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
          okClientBuilder.readTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
          okClientBuilder.writeTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
          return okClientBuilder.build();
      }
  </code></pre>
  
  <p>PostHandler.java </p>
  
  <pre><code>@Override
      public void callAPI(final Context context, final ApiClientResponse callback, Object arg0) {
          this.callback = callback;
          apiService.postLogIn(Login.username, Login.password).enqueue(new Callback&lt;LoginData&gt;() {
              @Override
              public void onResponse(Call&lt;LoginData&gt; call, Response&lt;LoginData&gt; response) {
                  if (response.isSuccessful()) {
                      LoginData loginData = response.body();
                      successLoginData = loginData;
                      successCallBack();
                  } else {
                      ApiErrorHandler.handleError(context, response, errorResponse);
                  }
              }
  
              @Override
              public void onFailure(Call&lt;LoginData&gt; call, Throwable t) {
                  ApiErrorHandler.handleError(context, t, errorResponse);
              }
  
              RetrofitErrorResponse errorResponse = new RetrofitErrorResponse() {
                  @Override
                  public void errorMessage(String errorMessage) {
                      failureCallBack(errorMessage);
                  }
  
                  @Override
                  public void tSyncError() {
  
                  }
  
                  @Override
                  public void invalidTokenError() {
  
                  }
              };
  
          });
      }
  </code></pre>
  
  <p>LoginData.java model class </p>
  
  <pre><code>@Generated("org.jsonschema2pojo")
  public class LoginData {
      @SerializedName("access_token")
      @Expose
      private String accessToken;
      @SerializedName("token_type")
      @Expose
      private String tokenType;
      @SerializedName("refresh_token")
      @Expose
      private String refreshToken;
      @SerializedName("expires_in")
      @Expose
      private long expiresIn;
      @SerializedName("scope")
      @Expose
      private String scope;
  
      // getter setter 
  }
  </code></pre>
  
  <p><strong>Here is my Spring boot application code example</strong>  </p>
  
  <p>MainApplicationClass.java</p>
  
  <pre><code>@SpringBootApplication
  public class MainApplicationClass {
      public static void main(String[] args) {
          SpringApplication.run(MainApplicationClass.class, args);
      }
  }
  </code></pre>
  
  <p>Controller.java </p>
  
  <pre><code>@RestController
  public class BlogController {
      @Autowired
      BlogRespository blogRespository;
      BlogMockedData blogMockedData = BlogMockedData.getInstance();
  
      @GetMapping("/blog")
      public List&lt;Blog&gt; index() {
          return blogMockedData.fetchBlogList();
      }
  
      @GetMapping("/blog/{id}")
      public Blog show(@PathVariable String id) {
          int blogId = Integer.parseInt(id);
          return blogMockedData.getBlogById(blogId);
      }
  
      @PostMapping(value = "/api/login",
              consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE},
              produces = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.APPLICATION_JSON_VALUE}
      )
      public LoginData postLogin(@RequestBody Map&lt;String, String&gt; body) {
          String userName = body.get("username");
          String password = body.get("password");
          return blogMockedData.getLoginToken(userName, password);
      }
  </code></pre>
  
  <p><strong>NOTE:</strong> If I hit spring boot POST service from POSTMAN i get below result </p>
  
  <p><a href="https://i.stack.imgur.com/sz5ZO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sz5ZO.png" alt="enter image descriptiIn here"></a></p>
  
  <p>But if I hit the POST service from my android client side it gets me the error</p>
  
  <blockquote>
    <p>"status":415,"error":"Unsupported Media
    Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content
    type 'application/x-www-form-urlencoded;charset=UTF-8' not
    supported","path":"/api/login"</p>
  </blockquote>