0

I am passing JSON data from jQuery to my Java controller and I am using @RequestBody, but I am getting an exception saying:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

The data which I am passing is:

myData = {
  "source": "CSS",
  "type": "CSS2",
  "typeValue": "value",
  "textarea_value": " desc"
}:

The AJAX call I am using to pass this data is:

$.ajax({
  url: './common/deleteData',
  type: 'POST',
  data: myData,
  success: function(data) {
    alert("Successfully Deleted Source..");
  },
  error: function(data) {}
});  

My Java Controller is as below

@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(@RequestBody SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException 
{
  LOGGER.entry("Deleting the Merge Preference Details");
  System.out.println(sourcedelete.getSource());
  return null;
}

My POJO object is as below:

public class SourceDelete {
  private String source;
  private String type;
  private String typeValue;
  private String textarea_value;
  //Setters and Getters
}

Can someone please help me figure out why I am getting this error and how I should fix it.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
vyas
  • 49
  • 8
  • *Writing a JSON REST service in Spring Boot is simple, as that's its default opinion when Jackson is on the classpath:* see https://www.baeldung.com/spring-boot-json – Scary Wombat Sep 25 '19 at 07:54
  • @ScaryWombat Iam not using Spring boot Iam using a Spring MVC Controller – vyas Sep 25 '19 at 07:55
  • `consumes = MediaType.APPLICATION_JSON_VALUE` – Scary Wombat Sep 25 '19 at 07:58
  • @ScaryWombat should i add this consumes to my controller – vyas Sep 25 '19 at 08:03
  • 1
    @vyas in your ajax call add dataType: "json" and contentType: "application/json; charset=utf-8" both are missing see https://stackoverflow.com/questions/18980841/how-to-call-ajax-request-with-json-response-using-jquery – Richard Elite Sep 25 '19 at 08:16
  • @DMK Thank You I had missed those.Now it was working – vyas Sep 25 '19 at 08:37

1 Answers1

0

remove the @RequestBody annotation,

@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException 
{
  LOGGER.entry("Deleting the Merge Preference Details");
  System.out.println(sourcedelete.getSource());
  return null;
}
madhepurian
  • 271
  • 1
  • 13
  • If i remove RequestBody,How my JSON will be mapped to Java Object – vyas Sep 25 '19 at 08:27
  • @vyas [refer this](https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for) – madhepurian Sep 25 '19 at 09:20