2016-11-30 9 views
0

こんにちは、私はWebサービスを初めて使いました。Javaで入力をJSONObjectとして受け取る簡単なWebサービスを作成する方法

私は、入力文字列を受け取り、別の文字列をeclipseを使用して返す単純なWebサービスを作成することができます。

しかし、それはJSONObjectに来るときにWebサービスを利用するために私を助けて、Webサービス

public class HelloWorld { 
private int rowNumber; 

public byte[] readJSON(JSONObject jsonObject) throws Exception 
{ 
    rowNumber=0; 
    File excelFile = new File("Test2.xlsx"); 
    OutputStream outStream = new FileOutputStream(excelFile); 
    XSSFWorkbook workbook = new XSSFWorkbook(); 
    XSSFSheet sheet = workbook.createSheet("TestSheet"); 
    XSSFRow row ; 
    XSSFCell cell; 
    JSONArray msg = (JSONArray) jsonObject.get("messages"); 
    Iterator<String> iterator = msg.iterator(); 
    while (iterator.hasNext()) { 

      row = sheet.createRow(rowNumber); 
     cell= row.createCell(0); 
     cell.setCellValue(iterator.next()); 
     rowNumber=rowNumber+1; 
    } 
    workbook.write(outStream); 
    outStream.close(); 

    Path path = Paths.get("Test2.xlsx"); 
    byte[] data = Files.readAllBytes(path); 
    return data; 

} 
public float addValue(float value) { 
    return (value + 10); 
} 
} 

を起動している間、私は、問題に直面しています。

SimpleDeserializerは、非直列化しようとしていたもので、期待されていない子要素を検出しました。このエラーは、私がクライアントを呼び出そうとしているときに発生しています。別のものはJSONObjectとしての入力パラメータですか?

+0

どのような問題がありますか? – Asu

+0

@Asu **デシリアライズしようとしていたもので、SimpleDeserializerが、期待されない子要素を検出しました。**このエラーは、クライアントを呼び出そうとしたときに発生しています。別のものはJSONObjectとしての入力パラメータですか? – Saurabh

+0

これを追加するには質問を編集する必要があります。 – Asu

答えて

0

ジャクソン:JSONに変換するか、JSONから解析できるJavaオブジェクトモデルクラスを定義する非常に優れたライブラリです。 多くの例が見つかります

1

パッケージjavax.ws.rsを使用することができます。

ここ
import javax.ws.rs.*; 

は、動作中のライブラリーの例の短い次のようになります。ここで はHTMLです:対応するJavaコードで

<div> 
    Welcome and happy <span id="today"></span>. 
    What's your name? 
    <input id="name" type="text" autofocus /> 
    <button id="submit" onclick="greet()">Submit</button> 
</div> 
<div id="greet"> 
    <!-- greeting goes here --> 
</div> 

<script> 
// fills in <span id="today">...</span> with today's day of the week 
// returned from /rest/today server endpoint 
function today() { 
    $.get("/rest/today", function(theday) { 
     $("#today").text(theday); 
    }); 
}; 
// fills in <div id="greeting">...</div> with the greeting 
// returned from calling the /rest/hello?name=... server endpoint 
// with the name from the input text box 
function greet() { 
    var thename = $("#name").val(); 
    $.get("/rest/hello", { name: thename }, function(thehello) { 
     $("#greet").text(thehello); 
    }) 
    .fail(function(jqXHR, textStatus, errorThrown) { 
     // displays server error message, e.g. if called with empty name 
     $("#greet").text(textStatus + ": " + errorThrown); 
    }); 
}; 
$(today); // execute today() after DOM is ready, see https://api.jquery.com/ready/ 
</script> 

</body> 
</html> 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.QueryParam; 
import javax.ws.rs.core.Response; 

/** 
* REST service that greets requests. 
* 
* This is a "root resource class" as explained in 
* https://jersey.java.net/documentation/latest/jaxrs-resources.html 
*/ 
@Path("/") 
public class HelloService { 
    @GET 
    @Path("/today") 
    public String today() { 
    return DayOfWeek.today(); 
    } 

    @GET 
    @Path("/hello") 
    public Response hello(@QueryParam("name") String name) { 
     if (name == null || name.isEmpty()) { 
      return Response.status(Response.Status.BAD_REQUEST).build(); 
     } else { 
      return Response.ok("hello " + name).build(); 
     } 
    } 
} 

JSONオブジェクトを操作するには、 Gson.toJson()を使用します。

String json = new Gson().toJson(some_object); 
return Response.ok(json, MediaType.APPLICATION_JSON).build(); 

私はこれが役に立つと願っています。

関連する問題