0

我是使用 Play2 框架的新手。这是我写的一个简单的控制器

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。我究竟做错了什么 ?

4

2 回答 2

0

为了快乐的开发,请尝试在chrome Postman中使用。或者如果你喜欢consoleway使用httpie

于 2013-11-08T06:43:25.083 回答
0

您的控制器看起来不错,但是您需要Content-type在 curl 请求上设置标头并-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
于 2013-11-07T23:45:04.447 回答