0

I am using play for the first time and am stuck in a maze. I am trying to store the value of top and left in the db along with the product name. I have managed to get pass the values to the controller but am not sure how to store it from there.

//controller
    @BodyParser.Of( play.mvc.BodyParser.Json.class )
        public static Result myMethod(){
            response().setContentType( "application/json" );
            JsonNode json = request().body().as(JsonNode.class);
            JsonNode jsonRequest = request().body().asJson();
            //left and top
            Double left= jsonRequest.findPath("Left").asDouble();
            Double top= jsonRequest.findPath("Top").asDouble();
            ObjectNode result = Json.newObject();
            System.out.println("left " +left+"\ntop "+top);
            result.put("status", "OK");
            return ok(result);
        }

This is my db schema

@Entity 
public class Position extends Model {

    @Id 
    public long id;

    @Required
    public long left;

    @Required
    public long top;

    public static Finder<Long,Position> find = new Finder<Long,Position>(Long.class, Layout.class);
}

How do I save it using ebeans???

4

1 に答える 1

0

私はこれに対する答えを得ました。json から Java オブジェクトに変換するだけです。

//controller
@BodyParser.Of( play.mvc.BodyParser.Json.class )
    public static Result myMethod(){
        response().setContentType( "application/json" );
        ObjectNode result = Json.newObject();
        JsonNode json = request().body().asJson();
        Position p = Json.fromJson(json, Position.class);
        System.out.println(json);// to view in controller
        p.save();

        result.put("status", "OK");
        return ok(result);
    }
于 2013-07-12T07:35:33.727 に答える