I have a simple webservice that check if username is present and returns a boolean
@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;
}
using a debugger, my isUsername does get set to true or false depending on input
my client looks as follows
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);
}
I get a 406 Not Acceptable exception
my spring configuration looks as follows
<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>