1

This is my web service method inside a Spring controller:

@RequestMapping(value = "/submit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public @ResponseBody Response submitAppication(@RequestBody String submitString) {
    System.out.println("***************************  Entering for submit part ********************************");
    Response response = Response.status(400).build();
    try {
        JSONObject jsonObject = new JSONObject(submitString);
        jsonObject.put("event", "submit");

        SMEvent sme = new SMEvent(HttpMethod.POST, jsonObject);

        String tid = Util.getTid(jsonObject);
        response = handleEvent(sme, getVerificationType(tid), tid);
    } catch (JSONException e) {
        //TODO log some exception here
    } finally {
        return response;
    }
}

This is my HTML page with a form:

<div th:if="${InstaPanState==1 and InstaPanStateStatus==0}">
    <body>
    <form action="#" th:action="@{/submit}" method="post">

        <label><b>Name</b></label>
        <input type="text" placeholder="Your Name" name="name"/><br/><br/>
        <br/>
        <label><b>Father's Name</b></label>
        <input type="text" placeholder="Father's Name" name="fname"/><br/>
        <br/><br/>
        <label><b>Contact Number</b></label>
        <input type="number" placeholder="Contact Number"/><br/><br/><br/>

        <div class="clearfix">
            <button type="button" class="cancelbtn">Cancel</button>
            <button type="submit" class="submitbtn">Submit</button>
        </div>
    </form>
    </body>
</div>

When I click the submit button it should call the mentioned web service method.

Currently I got the following exception and it is not calling the web service.

Looking up handler method for path /submit 2017-09-25 15:41:36.189 DEBUG 5405 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported

Mahozad
  • 18,032
  • 13
  • 118
  • 133
User120909
  • 39
  • 1
  • 6
  • @LucianovanderVeekens what should be the solution for this , I have also tried by adding
    Please help me as i am new in spring
    – User120909 Sep 25 '17 at 10:30
  • @LucianovanderVeekens tried and it is coming into the method but showing is a bad request since i am accepting it as string – User120909 Sep 25 '17 at 10:34
  • this https://stackoverflow.com/questions/22195065/how-to-send-a-json-object-using-html-form-data explain how to send json data from html form, otherwise you have to change server implementation using `RequestMethod.POST` and `@RequestParam` for each parameter that form send. – Lety Sep 25 '17 at 11:52

2 Answers2

1

You are POSTing an HTML form, but the web service is configured to consume JSON.

Change the web service method to:

@RequestMapping(value = "/submit", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody Response submitAppication(
          @RequestBody MultiValueMap<String, String> formParams) {
    // TODO: Extraction logic here
}
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • I got this : {"statusType":"BAD_REQUEST","entity":null,"entityType":null,"metadata":{},"status":400} with the solution what you suggest – User120909 Sep 25 '17 at 10:44
  • Sure... @RequestMapping(value = "/submit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public @ResponseBody Response submitAppication(@RequestBody MultiValueMap submitString) { Response response = Response.status(400).build(); JSONObject jsonObject = new JSONObject(submitString); jsonObject.put("event", "submit"); ///// Some code return response; } – User120909 Sep 25 '17 at 13:05
  • @User120909 I had an error in `consumes`, use `MediaType.APPLICATION_FORM_URLENCODED_VALUE` – Luciano van der Veekens Sep 25 '17 at 13:08
  • MediaType.APPLICATION_FORM_URLENCODED_VALUE This mediatype is not supporting for intelij it is showing error – User120909 Sep 25 '17 at 13:08
  • @User120909 you should also remove `@Produces`, because that mixes Java EE and Spring. The `RequestMapping` annotation itself has a `produces` field, use that one. – Luciano van der Veekens Sep 25 '17 at 13:10
  • I have tried by removing this and i got bad request issue: {"statusType":"BAD_REQUEST","entity":null,"entityType":null,"metadata":{},"status":400} – User120909 Sep 25 '17 at 13:14
  • Code is : @RequestMapping(value = "/submit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED) public @ResponseBody Response submitAppication(@RequestBody MultiValueMap submitString) { System.out.println("*************************** Entering for submit part *********************************" + submitString); Response response = Response.status(400).build(); – User120909 Sep 25 '17 at 13:14
  • @User120909 looks like you're deliberately returning status 400. Does the `System.out.println` statement print anything? That would mean the code works. – Luciano van der Veekens Sep 25 '17 at 13:16
-1

Please try the below.

  @PostMapping(value = "/submit")
        public ResponseEntity<UserSetail> createUser( @RequestBody UserDetail user) 
        {
    new ResponseEntity<UserSetail>(user, HttpStatus.CREATED);
        }