2016-08-10 57 views
2

無論我嘗試向調度程序servlet發出的請求都返回HTTP 415錯誤。 Content-Type在請求中設置爲application/json。Spring MVC @RequestBody給出了415錯誤

看來消息轉換器並沒有將請求映射到對象。

我都在我的POM傑克遜依賴關係:

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>${jackson-databind-version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.module</groupId> 
     <artifactId>jackson-module-jaxb-annotations</artifactId> 
     <version>2.4.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 
    <dependency> 
     <groupId>com.sun.xml.bind</groupId> 
     <artifactId>jaxb-impl</artifactId> 
     <version>2.2.7</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.xbean</groupId> 
     <artifactId>xbean-spring</artifactId> 
     <version>3.7</version> 
    </dependency> 

控制器類:

@RequestMapping(value = "/login", method = RequestMethod.POST, headers = {"Accept=application/json"}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 
@ResponseBody 
public DriverLoginResponseDTO login(@RequestBody MyDTO dto) 
{ 
    System.out.println("login method hit!"); 
    LoginResponseDTO resp = null; 

    try { 
     resp = loginService.processLogin(dto); 
    } 
    catch (Exception cde) { 
     resp = new LoginResponseDTO(); 
     resp.setMessage("ERROR"); 
    } 

    // Return response object 
    return resp; 
} // login 

我試着加入接受,消耗,在@RequestMapping產生無濟於事。

我可以得到HttpServletRequest的正確的JSON響應的方法參數:

@RequestMapping(value = "/login3", method = RequestMethod.POST) 
@ResponseBody 
public DriverLoginResponseDTO login3(HttpServletRequest request) 
{ 
    String line; 
    StringBuilder sb = new StringBuilder(); 

    InputStream is; 
    try { 
     is = request.getInputStream(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
     while ((line = reader.readLine()) != null) { 
      sb.append(line).append("\n"); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    System.out.println("login3 method hit!"); 
    System.out.println("JSON string received: " + sb.toString()); 

    LoginResponseDTO resp = null; 

    try { 
     resp = loginService.processLogin(new MyDTO()); 
    } 
    catch (Exception cde) { 
     resp = new LoginResponseDTO(); 
     resp.setMessage("ERROR"); 
    } 

    // Return response object 
    return resp; 
} // login 

這其中顯示了當寫入到System.out正確的JSON字符串。有些東西沒有將JSON請求映射到上面第一個「login」方法中的RequestBody對象,但我看不到缺少的東西。

我不知道這是否是相關的Websphere,但也引發了以下錯誤:

SRVE8094W:警告:不能設置標題。響應已經提交。

+0

請添加樣本JSON消息,並添加調用服務的代碼。您也可以嘗試使用郵遞員等其他客戶端來調用您的服務。 – reos

+0

沒有垃圾郵件,我想要你的ajax請求代碼。 – Byeon0gam

+0

_「這一個顯示正確的JSON字符串」_這是無關緊要的。最重要的是媒體類型。 – zeroflagL

回答

0

嘗試設置consumes = "application/json"而不是headers = {"Accept=application/json"}@RequestMapping

+1

我已經有了在控制器類。我添加了Accept頭部以查看它是否會有所幫助 - 沒有運氣。 –

相關問題