2013-09-11 69 views
0

我有POST方法的ajax請求。請求將Json數據發佈到服務器。我想這個JSON解析到一個像這樣的字符串:Json在澤西島中的字符串

@POST 
@Path("file/save") 
@Consumes(MediaType.APPLICATION_JSON) 
public void save(String content) { 
} 

我得到這個錯誤每次我嘗試對服務器的請求:

「錯誤400無法反序列化java.lang.String中的實例出START_OBJECT令牌」

下面是詳細的POST請求:

Request URL: http://localhost:8181/file/save 
Request Method:POST 
Status Code:400 Bad Request 

Request Headers 
Accept:*/* 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:379 
Content-Type:application/json 
Cookie:JSESSIONID=0AFC5FF7F201391DD67CCB0DA1BCD25C 
Host:localhost:8181 
Origin:http://localhost:8181 
Referer:http://localhost:8181 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 
X-Requested-With:XMLHttpRequest 

Query String Parameters 

Request Payload 
<my json data> 

Response Headers 
Cache-Control:must-revalidate,no-cache,no-store 
Content-Length:1369 
Content-Type:text/html;charset=ISO-8859-1 
Date:Wed, 11 Sep 2013 03:11:03 GMT 
Server:Apache-Coyote/1.1 

我該如何解決這個問題?謝謝!

+1

**,**,**是產生400的HTTP POST請求?請包括所有標題和正文。 –

回答

1

問題是您的JAX-RS實現(在這種情況下爲澤西島)不知道如何將application/json內容映射到String。您需要註冊MessageBodyReader<String>實施,並註明@Consumes(MediaType.APPLICATION_JSON)

補遺

實現一個MessageBodyReader一個轉換爲String是直截了當的。

@Provider 
@Consumes(MediaType.APPLICATION_JSON) 
public class JsonStringReader implements MessageBodyReader<String> 
{ 
    @Override 
    public boolean isReadable(
     Class<?> type, 
     Type genericType, 
     Annotation[] annotations, 
     MediaType mediaType 
    ) 
    {return type == String.class;} 

    @Override 
    public String readFrom(
     Class<String> type, 
     Type genericType, 
     Annotation[] annotations, 
     MediaType mediaType, 
     MultivaluedMap<String, String> httpHeaders, 
     InputStream entityStream 
    ) throws IOException, WebApplicationException 
    { 
     final String charsetName = mediaType.getParameters().get(MediaType.CHARSET_PARAMETER); 
     final Charset charset = charsetName == null ? Charset.forName("UTF-8") : Charset.forName(charsetName); 
     final StringBuilder builder = new StringBuilder(); 
     final Reader reader = new InputStreamReader(entityStream, charset); 
     final CharBuffer buffer = CharBuffer.allocate(1024); 
     while (reader.read(buffer) != -1) 
     { 
     buffer.flip(); 
     builder.append(buffer); 
     buffer.clear(); 
     } 
     return builder.toString(); 
    } 
} 

有幾種方法可以通過JAX-RS實現註冊實體提供者。這可能與澤西更簡單的方法是添加以下到您的web.xml

<init-param> 
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>pkg.that.contains.the.messagereader</param-value> 
</init-param> 

上創建新澤西實體提供進一步的信息可以在這裏找到:https://jersey.java.net/documentation/latest/message-body-workers.html

上配置新澤西州的更多信息可以在這裏找到:http://jersey.java.net/nonav/apidocs/1.8/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html

+0

你能告訴我該怎麼做嗎? –

+0

在文體上,有一個名爲「文件/保存」的資源只能發佈到不是真正的RESTful。在真正的REST風格中,您可能有一個名爲「files」的資源,GET從該資源返回現有內容URI的列表,並從中添加新資源並通過HTTP 201創建的響應返回其URI。 – reggert