Playframework - JAVA: Receive Record Data

What am I doing wrong?

I am trying to use POST data but always do not work. So, how do you get the first response to the conditional descriptor badRequest ("Expceting some data") to be zero .

Routes

POST   /pedidos/novo    controllers.Pedidos.cadastraPedidoNoBanco

Action in my controller

@BodyParser.Of(BodyParser.Json.class)
    public static Result cadastraPedidoNoBanco(){
        JsonNode data = request().body().asJson();
        if(data == null){
            return badRequest("Expceting some data");
        } else {
            return ok(data);
        }
    }

Another failure test

So this did not happen, but I did not know why the answer is this:

Source

public static Result cadastraPedidoNoBanco(){
    Map<String,String[]> data = request().body().asFormUrlEncoded();
    if(data == null){
        return badRequest("Expceting some data");
    } else {
        String response = "";
        for(Map.Entry value : data.entrySet()){
            response += "\n" + value.getValue();
        }
        return ok(response);
    }
}

Answer

[Ljava.lang.String;@5817f93f
[Ljava.lang.String;@decc448
[Ljava.lang.String;@334a5a1c
[Ljava.lang.String;@5661fe92
[Ljava.lang.String;@3b904f8c
[Ljava.lang.String;@7f568ee0
[Ljava.lang.String;@bbe5570

Curl

curl "http://localhost:9000/pedidos/novo" -H "Origin: http://localhost:9000" -H "Accept-Encoding: gzip,deflate" -H "Accept-Language: en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Cache-Control: max-age=0" -H "Referer: http://localhost:9000/pedidos/novo" -H "Connection: keep-alive" -H "DNT: 1" --data "nome_cliente=nj&telefone_cliente=jn&rua_cliente=jkn&num_cliente=kjn&comp_cliente=kjn&cep_cliente=kjn&bairro_cliente=jnk" --compressed
+4
source share
2 answers

DynamicFormmore convenient (nota bene, delete the annotation @BodyParser, you do not need it):
(samples for playing 2.3.x)

import play.data.DynamicForm;
import play.data.Form;

public static Result cadastraPedidoNoBanco() {

    DynamicForm form = Form.form().bindFromRequest();

    if (form.data().size() == 0) {
        return badRequest("Expceting some data");
    } else {
        String response = "Client " + form.get("nome_cliente") + "has phone number " + form.get("telefone_cliente");

        return ok(response);
    }
}

But better...

Option

, , .., ...

//Pedido.java

package forms;

import play.data.validation.Constraints;

public class Pedido {

    @Constraints.Required()
    @Constraints.MinLength(3)
    public String nome_cliente;

    @Constraints.Required()
    @Constraints.MinLength(9)
    public String telefone_cliente;

    @Constraints.Required()
    public Long some_number;
    //etc...
}

:

import java.util.List;
import java.util.Map;

import play.data.Form;
import forms.Pedido;
import play.data.validation.ValidationError;
import play.i18n.Messages;

public class Pedidos extends Controller {


    public static Result cadastraPedidoNoBanco() {
        Form<Pedido> form = Form.form(Pedido.class).bindFromRequest();

        if (form.hasErrors()) {
            String errorsMsg = "There are " + form.errors().size() + " errors... \n";

            // Of course you can skip listing the errors
            for (Map.Entry<String, List<ValidationError>> entry : form.errors().entrySet()) {
                String errorKey = entry.getKey();
                List<ValidationError> errorsList = entry.getValue();
                for (ValidationError validationError : errorsList) {
                    errorsMsg += errorKey + " / " + Messages.get(validationError.message()) + "\n";
                }
            }

            return badRequest(errorsMsg);
        }

        Pedido data = form.get();
        String response = "Client " + data.nome_cliente + " has phone number " + data.telefone_cliente;

        return ok(response);
    }

}

, -, ...

if (form.hasErrors()) {
    return badRequest(views.html.yourViewWithForm.render(form));
}
+9

post. , " " . Playframework . [0], .

, post param "name"

request().body().asFormUrlEncoded().get("name")[0];
+7

All Articles