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.

6
  • Writing a JSON REST service in Spring Boot is simple, as that's its default opinion when Jackson is on the classpath: see baeldung.com/spring-boot-json Commented Sep 25, 2019 at 7:54
  • @ScaryWombat Iam not using Spring boot Iam using a Spring MVC Controller Commented Sep 25, 2019 at 7:55
  • consumes = MediaType.APPLICATION_JSON_VALUE Commented Sep 25, 2019 at 7:58
  • @ScaryWombat should i add this consumes to my controller Commented Sep 25, 2019 at 8:03
  • 1
    @vyas in your ajax call add dataType: "json" and contentType: "application/json; charset=utf-8" both are missing see stackoverflow.com/questions/18980841/… Commented Sep 25, 2019 at 8:16

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If i remove RequestBody,How my JSON will be mapped to Java Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.