我有一個簡單的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>
關於如何解決它 – user373201
你的錯是你的客戶,或者你如何使用您的客戶端的任何想法。 – Raedwald
我已經提供了客戶端代碼,如果不是太麻煩你能告訴我我犯了什麼錯誤 – user373201