1

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>

2 Answers 2

2

According to the HTTP standard, status 406 (Not Acceptable) means

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

Your controller method says

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

So your controller will serve only requests that request Accept=application/xml. Requests for all other representations of the resource will result in in 406 response status.

I conclude that your client request (GET) is not asking for application/xml.


Your client asks for a ResponseEntity<Boolean> from the RestTemplate. So the Accept header sent to the server will list the media types of all the message-converters of the client that can convert a Boolean. Your only message converter is jaxbMarshaller. I'm not familiar with that converter, but I guess you have not correctly configured its Spring-bean so it will convert from a Boolean.

Sign up to request clarification or add additional context in comments.

3 Comments

any ideas on how to fix it
Your fault is in your client, or how you are using your client.
I have provided the client code, if not too much trouble can you tell me what mistake am i making
0

Your client needs to send application/xml accept headers:

...
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 Comment

I have updated the client to include setAccept but still get the exact same result. Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.