2014-12-02 18 views
-1

我有以下問題:如何檢索請求的身體上播放框架2.0控制器

當我嘗試使用在https://www.playframework.com/documentation/2.3.x/JavaJsonActions

所示的例子它建議我使用下面的語句:

@BodyParser.Of(BodyParser.Json.class) 
public static Result sayHello() { 
    JsonNode json = request().body().asJson(); 
    String name = json.findPath("name").textValue(); 
    if(name == null) { 
    return badRequest("Missing parameter [name]"); 
    } else { 
    return ok("Hello " + name); 
    } 
} 

但是,當我嘗試編譯我得到一個「無法找到符號」在request()方法。是我還是我忘記了一些導入或方法改變,我在看舊代碼?我的代碼如下波紋管:

package controllers; 

import play.*; 
import play.mvc.*; 
import views.html.*; 

import models.Evento; 
import models.Pessoa; 
import models.Conteudo; 
import org.springframework.beans.factory.annotation.Autowired; 
import play.data.Form; 
import play.libs.Json; 
import play.mvc.Result; 
import services.EventoService; 
import services.PessoaService; 
import services.ConteudoService; 
import views.html.index; 

import java.util.List; 

import play.api.Application; 
//import org.apache.commons.io.*; 

import com.fasterxml.jackson.databind.JsonNode; 
import com.fasterxml.jackson.databind.JsonMappingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import play.mvc.BodyParser; 

@org.springframework.stereotype.Controller 
public class EventApp { 

    /* Autowired Classes and other results here */ 

    @BodyParser.Of(BodyParser.Json.class) 
    public Result addEvento() { 

     JsonNode json = request().body().asJson(); 
     Evento evento = new Evento(); 

     evento.nomeEvento = json.findPath("nomeEvento").textValue(); 
     evento.dataEvento = json.findPath("dataEvento").textValue(); 
     evento.enderecoEvento = json.findPath("enderecoEvento").textValue(); 
     evento.tipoEvento = json.findPath("tipoEvento").intValue(); 
     evento.perfilEvento = json.findPath("perfilEvento").intValue(); 
     evento.vCardapio = (char)json.findPath("vCardapio").textValue(); 
     evento.vPhotoreel = (char)json.findPath("vPhotoreel").textValue(); 

     if(evento.nomeEvento == null) { 
      return play.mvc.Controller.ok("Missing parameter [name]"); 
     } else { 
      return play.mvc.Controller.ok("Json Recebido campeao"); 
     } 

    } 


} 
+0

你爲什麼不擴大你的EventApp控制器與'play.mvc.Controller'? – biesior 2014-12-02 17:59:09

回答

相關問題