使用Scala Spray httpx编组自定义案例类

时间:2015-10-12 17:58:08

标签: scala spray spray-json spray-client

我试图将一个字符串编组到我拥有的自定义案例类中。这是我试图使用的代码

public void GenerateTXT() { 
   Connection conn = null;
   Statement stmt = null;
   try{ 
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      stmt = conn.createStatement();

      File file = new File("/path/to/file/filename.txt");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

      String sql = "SELECT id, name, amount FROM Employee";
      ResultSet rs = stmt.executeQuery(sql);

      while(rs.next()){
         int id  = rs.getInt("id");
         int age = rs.getString("name");
         String first = rs.getInt("amount");
         bw.append(id+"|"+age+"|"+first+"\n");
          } 
      rs.close();
      bw.close();
   }catch(SQLException se){ 
      se.printStackTrace(); 
   }catch(Exception e){
      e.printStackTrace();
   }finally{ 
      try{ 
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){ 
      } 
      try{ 
         if(conn!=null)
            conn.close();
      }catch(SQLException se){ 
         se.printStackTrace(); 
      } 
   } 
} 
} 

这是我得到的错误:

import spray.httpx.SprayJsonSupport._
import NflWeekJsonProtocol._
    path("playerScore") {
      get {
        parameters('gsisId.as[String] ?, 'week.as[NflWeek] ?, 'playerId.as[String]).as(PlayerScoreRequest) {
          playerScoreRequest : PlayerScoreRequest =>

        }
      }
    }

[error] /home/chris/dev/suredbits-dfs/src/main/scala/com/suredbits/dfs/nfl/scoring/NflPlayerScoringService.scala:40: too many arguments for method parameters: (pdm: spray.routing.directives.ParamDefMagnet)pdm.Out [error] parameters('gsisId.as[String] ?, 'week.as[NflWeek] ?, 'playerId.as[String]).as(PlayerScoreRequest) { [error] ^ [error] one error found 表示序列化案例类NflWeekJsonProtocol的方法。我认为这就是我需要做的才能使这个工作,我错过了什么?

编辑:

NflWeek

1 个答案:

答案 0 :(得分:0)

spray docs建议你在使用自定义反序列化器时需要parens而不是方括号,.as(NflWeek)