2013-05-10 29 views
0

我偶然發現了一個相當奇怪的問題。搜索沒有給出任何答案,所以我想問它在這裏...刪除ID大於1000的問題

我正在創建一個程序,與web服務(休息)進行通信。在客戶端,我有這個方法,刪除樣本:

public void remove(int id) throws UniformInterfaceException { 
    webResource.path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete(); 
} 

在服務器端:

@DELETE 
@Path("{id}") 
public void remove(@PathParam("id") Integer id) { 
    System.out.println("delete sample id = " + id); 
    super.remove(super.find(id)); 
} 

現在,這似乎與< 1000(ID在顯示的所有標識工作輸出)。一旦超過1000,出於某種原因,似乎有一千個分離器在工作?這會導致客戶端出現以下錯誤:

com.sun.jersey.api.client.UniformInterfaceException: DELETE http://localhost:8080/myname/webresources/entities.samples/1,261 returned a response status of 404 Not Found 

爲什麼它在URI中使用1,261而不是1261?或者我在某個地方犯了什麼愚蠢的錯誤?

在此先感謝。

+1

你爲什麼不只是調用將String.valueOf(ID),MessageFormat中似乎是這是一個開銷 – hoaz 2013-05-10 18:22:54

回答

3

這裏的問題是MessageFormat類使用語言環境來格式化數字。從javadoc(位於頂部表格的子格式創建列下),「NumberFormat.getIntegerInstance(getLocale())」。這包括一些區域設置的千位分隔符。考慮以下幾點:

java> MessageFormat.format("{0}", new Object[]{Integer.valueOf(999)}) 
String res0 = "999" 

java> MessageFormat.format("{0}", new Object[]{Integer.valueOf(1000)}) 
String res1 = "1,000" 

你可以從你在這種情況下使用的MessageFormat來Integer.toString有選擇性地更改:

java> Integer id = 999 
Integer id = 999 

java> id.toString() 
String res3 = "999" 

java> id = 1000 
Integer id = 1000 

java> id.toString() 
String res4 = "1000"