2016-11-30 43 views
0

你好,我是新的Web服務。如何創建簡單的Web服務,將輸入作爲JSONObject在java中

我能夠創建簡單的Web服務,它接受輸入字符串並使用eclipse返回另一個字符串。

但是,當涉及到JSONObject我面臨的問題,同時調用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); 
} 
} 

所以幫我使用的Web服務。

SimpleDeserializer在試圖反序列化的東西中遇到了一個不期望的子元素。當我嘗試調用客戶端時出現此錯誤。而另一個輸入參數爲JSONObject的參數是允許的嗎?

+0

有什麼問題? – Asu

+0

@Asu ** SimpleDeserializer在嘗試反序列化時遇到了不期望的子元素。**當我嘗試調用客戶端時,出現此錯誤。而另一個輸入參數爲JSONObject的參數是允許的嗎? – Saurabh

+0

你應該編輯問題來添加這個,更好的答案會得到更多的upvotes並且更加明顯。 – Asu

回答

0

您可以嘗試使用Jackson:您可以在其中定義您可以轉換爲JSON或從JSON解析的Java對象模型類的非常好的庫。 你會發現很多的例子

1

你可以使用包裹包javax.ws.rs

import javax.ws.rs.*; 

這裏將是一個簡單的例子中作用的庫的: 下面是HTML:

<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> 

隨着對應的Java代碼:

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(); 

我希望這對我很有幫助!

相關問題