spring rest web service返回布尔值

时间:2014-01-22 12:47:46

标签: java rest spring-mvc

我有一个简单的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根据输入

设置为true或false

我的客户端如下所示

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>

2 个答案:

答案 0 :(得分:1)

根据HTTP标准,status 406 (Not Acceptable)表示

  

请求标识的资源只能生成响应实体,这些响应实体的内容特征根据请求中发送的接受标头不可接受。

你的控制器方法说

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

因此,您的控制器将仅提供请求Accept=application/xml的请求。对资源的所有其他表示的请求将导致406响应状态。

我的结论是,您的客户请求(GET)并未要求application/xml


您的客户要求ResponseEntity<Boolean>提供RestTemplate。因此,发送到服务器的Accept标头将列出可以转换Boolean的客户端的所有消息转换器的媒体类型。您唯一的邮件转换器是jaxbMarshaller。我不熟悉那个转换器,但我猜你没有正确配置它的Spring-bean,所以它将从Boolean转换。

答案 1 :(得分: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");
...