0

I've an HTML form where I'm getting 2 inputs, which is submitted to a spring boot REST api. But in this simple application, I'm still receiving null as request in backend api.

Form

<div>
    <label>alphaID</label>  
    <div>
        <input id="alphaID" name="alphaID" type="text"/>
    </div>
</div>

<div>
    <label class="col-md-4 control-label">Domain Name</label>  
    <div class="col-md-4">
        <input id="domain" name="domain" type="text"/>
    </div>
</div>

Upon submit, I'm calling ajax call, like:

function formSubmit() {
    $("#productForm").submit(function(e) {
        e.preventDefault();
        var requestJson = createRequestJSON();
        var url = config.myurl;
        $.ajax({
            url: url,
            type : "POST",
            data: JSON.stringify(requestJson),
            success: function( data, textStatus, jQxhr ) {
                console.log("sucess: " + data);
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log( "error: " + errorThrown );
            }
        });
    });
}

The backend is a spring-boot application with REST call:

@RequestMapping(value = "/validate", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public Map<String, List<String>> validate(@Valid MyDTO myDTO) {

    System.out.println(myDTO.getId());          // GETTING null
    System.out.println(myDTO.getDomain());      // GETTING null

}

MyDTO.java

public class MyDTO {
    @JsonProperty("alpha_id")
    private String alphaID;

    @JsonProperty("domain")
    private String domain;

    ....
}
reiley
  • 3,759
  • 12
  • 58
  • 114

2 Answers2

1

Change your Content-Type to

consumes = MediaType.APPLICATION_JSON_VALUE

Add @RequestBody annotation

public Map<String, List<String>> validate(@Valid @RequestBody MyDTO myDTO)

Make sure you are calling proper URL and sending proper content-type from your browser request too.

Nitin Zadage
  • 633
  • 1
  • 9
  • 27
  • From browser then, I should send `application/json`, right? – reiley Dec 10 '19 at 12:14
  • I made the changes, I'm getting error: `Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]` – reiley Dec 10 '19 at 12:18
  • Refer this post to add `content-type` and `Accept` header in Ajax call. https://stackoverflow.com/questions/9754767/cannot-set-content-type-to-application-json-in-jquery-ajax – Nitin Zadage Dec 10 '19 at 13:11
0

It might be interesting to see if your requestJson actually has the correct format in order for the MyDTO to consume it.

You also don't have to Json.stringify you data. When you do this, you basically just send an string to the backend. The backend does not know that it hast to parse this string to get a valid document. You either just send the JavaScript object directly in the data property or you change the API to expect a String and parse it in the function later.

Simon
  • 194
  • 13