gpt4 book ai didi

java - 游戏框架-JAVA : Get Post Data

转载 作者:搜寻专家 更新时间:2023-11-01 01:04:52 25 4
gpt4 key购买 nike

我做错了什么??

我正在尝试任何方法来获取 POST 数据,但总是行不通。所以它是如何空执行第一个条件响应是badRequest("Expceting some data")

路线

POST   /pedidos/novo    controllers.Pedidos.cadastraPedidoNoBanco

我的 Controller 中的 Action

@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);
}
}

另一个失败测试

所以这没有发生任何错误,但我不知道为什么响应是这样的:

来源

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);
}
}

响应

[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

最佳答案

DynamicForm 更舒服(注意,删除 @BodyParser 注释,你不需要它):
(Play 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);
}
}

更好...

选项将为您的表单创建专用表单,因此您可以使用内置约束、类型等验证字段,示例表单看起来像...

app/forms/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));
}

关于java - 游戏框架-JAVA : Get Post Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com