[java] how to get param in method post spring mvc?

I'm using spring mvc. And I can't get param from url when method = post. But when I change method to GET, so I can get all param.

This is my form:

<form method="POST" action="http://localhost:8080/cms/customer/create_customer" id="frmRegister" name ="frmRegister" enctype="multipart/form-data">
    <input class ="iptRegister" type="text" id="txtEmail" name="txtEmail" value="" />
    <input class ="iptRegister" type="password" id="txtPassword" name="txtPassword" value="" />
    <input class ="iptRegister" type="text" id="txtPhone" name="txtPhone" value="" />

    <input type="button" id="btnRegister" name="btnRegister" value="Register" onclick="" style="cursor:pointer"/>
</form>

This is my controller:

@RequestMapping(value= "/create_customer", method = RequestMethod.POST)
@ResponseBody
public String createCustomer(HttpServletRequest request, 
        @RequestParam(value="txtEmail", required=false) String email, 
        @RequestParam(value="txtPassword", required=false) String password, 
        @RequestParam(value="txtPhone", required=false) String phone){

    ResultDTO<String> rs = new ResultDTO<String>();
    rs.setStatus(IConfig.SHOW_RESULT_SUCCESS_ON_MAIN_SCREEN);
    try{
        Customer c = new Customer();
        c.setEmail(email);
        c.setPassword(password);
        c.setPhone(phone);
        customerService.insert(c);
        rs.setData("Insert success");
    }catch(Exception ex){
        log.error(ex);
        rs.setStatus(IConfig.SHOW_RESULT_ERROR_ON_MAIN_SCREEN);
        rs.setData("Insert failure");
    }
    return rs.toString();
}

How can I resolved this?

This question is related to java spring spring-mvc

The answer is


You should use @RequestParam on those resources with method = RequestMethod.GET

In order to post parameters, you must send them as the request body. A body like JSON or another data representation would depending on your implementation (I mean, consume and produce MediaType).

Typically, multipart/form-data is used to upload files.


It also works if you change the content type

    <form method="POST"
    action="http://localhost:8080/cms/customer/create_customer"
    id="frmRegister" name="frmRegister"
    enctype="application/x-www-form-urlencoded">

In the controller also add the header value as follows:

    @RequestMapping(value = "/create_customer", method = RequestMethod.POST, headers = "Content-Type=application/x-www-form-urlencoded")

When I want to get all the POST params I am using the code below,

@RequestMapping(value = "/", method = RequestMethod.POST)
public ViewForResponseClass update(@RequestBody AClass anObject) {
    // Source..
}

I am using the @RequestBody annotation for post/put/delete http requests instead of the @RequestParam which reads the GET parameters.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to spring-mvc

Two Page Login with Spring Security 3.2.x ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized The type WebMvcConfigurerAdapter is deprecated RestClientException: Could not extract response. no suitable HttpMessageConverter found Spring boot: Unable to start embedded Tomcat servlet container UnsatisfiedDependencyException: Error creating bean with name 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)