2014-01-22 53 views
2

我有一個簡單的Web服務是檢查,如果用戶名存在,且使用調試器返回boolean彈簧安置web服務返回布爾值

@RequestMapping(method=RequestMethod.GET, value="/isUsernameTaken/{username}", headers={"Accept=application/xml"}) 
public @ResponseBody Boolean isUsernameTaken(@PathVariable String username) throws FinderException { 
    boolean isUsernameTaken = userManager.isUsernameTaken(username); 
    return isUsernameTaken; 
} 

,我isUsername不會獲取設置爲根據輸入真或假

我的客戶看起來如下

public static void main(String[] args) { 
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/ws/rest-servlet.xml"); 
    RestTemplate restTemplate = ctx.getBean("restTemplate", RestTemplate.class); 

    String plainCreds = "rest:123456"; 
    byte[] plainCredsBytes = plainCreds.getBytes(); 
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
    String base64Creds = new String(base64CredsBytes); 

    HttpHeaders headers = new HttpHeaders(); 
    headers.add("Authorization", "Basic " + base64Creds); 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); 

    HttpEntity<String> request = new HttpEntity<>(headers); 
    ResponseEntity<Boolean> response = restTemplate.exchange("http://localhost:8031/WebService/service/isUsernameTaken/admin", HttpMethod.GET, request, Boolean.class); 
    Boolean msg = response.getBody(); 


    System.out.print(msg); 
} 

,我收到了406不可接受的例外

我的彈簧配置看起來如下

<context:component-scan base-package="com.merc.ws.service" /> 

<import resource="../business-config.xml" /> 

<mvc:annotation-driven /> 

<!-- To enable @RequestMapping process on type level and method level --> 
<!-- Convert Input --> 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > 
    <property name="messageConverters"> 
     <list> 
      <ref bean="marshallingConverter" /> 
     </list> 
    </property> 
</bean> 

<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 
    <property name="marshaller" ref="jaxbMarshaller" />  
    <property name="unmarshaller" ref="jaxbMarshaller" /> 
</bean> 

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>com.merc.domain.User</value> 
     </list> 
    </property> 
</bean> 

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> 
    <property name="messageConverters"> 
     <list>  
      <ref bean="marshallingConverter"/> 
     </list> 
    </property> 
</bean> 

回答

1

根據HTTP標準,status 406 (Not Acceptable)意味着

該請求所標識的資源只能能夠產生具有根據內容無法接收響應實體的在請求中發送接受標頭。

你的控制器的方法說

@RequestMapping(method=RequestMethod.GET, value="/isUsernameTaken/{username}", headers={"Accept=application/xml"}) 

所以您的控制器將成爲只要求Accept=application/xml請求。對資源的所有其他表示的請求將導致406響應狀態。

我斷定您的客戶請求(GET)沒有要求application/xml


您的客戶端請求從RestTemplate一個ResponseEntity<Boolean>。因此,發送到服務器的Accept標題將列出可以轉換Boolean的客戶端的所有消息轉換器的媒體類型。您唯一的信息轉換器是jaxbMarshaller。我不熟悉那個轉換器,但我想你沒有正確地配置它的Spring bean,所以它會從Boolean轉換。

+1

關於如何解決它 – user373201

+0

你的錯是你的客戶,或者你如何使用您的客戶端的任何想法。 – Raedwald

+1

我已經提供了客戶端代碼,如果不是太麻煩你能告訴我我犯了什麼錯誤 – user373201

0

你的客戶端需要發送application/xml接受頭:

... 
HttpHeaders headers = new HttpHeaders(); 
headers.add("Authorization", "Basic " + base64Creds); 
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); 
// done manually that'd be 
// headers.add("Accept", "application/xml"); 
... 
+1

我已經更新了客戶端以包含setAccept,但仍然得到完全相同的結果。請幫忙 – user373201