我是使用Play2 Framework的新手。这是一个我写过的简单控制器
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import models.*;
import java.util.*;
import play.libs.Json;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Users extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result register() {
JsonNode json = request().body().asJson();
String firstName=json.findPath("first_name").asText();
String lastName=json.findPath("last_name").asText();
String mobileNumber=json.findPath("mobile").asText();
String address=json.findPath("address").asText();
String userName=json.findPath("user_name").asText();
String password=json.findPath("password").asText();
ObjectNode result = Json.newObject();
result.put("status", "FAILIURE");
result.put("message", "Missing parameter [name]");
return ok("meow");
}
public static Result login() {
return TODO;
}
public static Result logout(final Long id) {
return TODO;
}
}
我正在打卷曲
curl -i -H "Accept: application/json" -X PUT -d "{"first_name": "simon","last_name": "bolliver","mobile": "12345","address": "23 Donwton","user_name": "simon","password": "levy"}" http://localhost:9000/user/register
我在第二行收到NullPointerException。我究竟做错了什么 ?
答案 0 :(得分:0)
您的控制器看起来很好,但您需要在curl请求上设置Content-type
标头,并使用-d
而不是'
包裹"
参数。
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"first_name": "simon","last_name": "bolliver","mobile": "12345","address": "23 Donwton","user_name": "simon","password": "levy"}' http://localhost:9000/user/register
答案 1 :(得分:0)