用JSON玩2

时间:2013-11-07 12:45:27

标签: java playframework-2.0

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

2 个答案:

答案 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)

对于快乐发展,请尝试使用chrome Postman。或者,如果您希望consoleway使用httpie