spring返回带请求的xml pojo类

问题描述 投票:0回答:1

我不断收到 HTTP 状态 406 – 不可接受。唯一有效的是字符串。我正在使用 spring 5 和 java 11

这是我的@RestController,我试图返回 POJO 类的 xml

 @RestController
public class GetCountries extends AbstractResource  {
    private final CountriesToCountryLinksTransformer countryLinksTransformer = new CountriesToCountryLinksTransformer();


    @RequestMapping(value = "/service", produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<CountryLinks> getCountries() throws JAXBException {
        setDefaultRequestAuditingAttributes();
        List<String> countryList = new ArrayList<>(getAuthorized().getAvailableCountries());
        CountryLinks countries = countryLinksTransformer.transform(countryList, uriInfo, request.getRequestURL());

      request.setAttribute(RequestConstants.SUCCESS_STATUS, true);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_XML);

       return new ResponseEntity<>(countries, responseHeaders, HttpStatus.OK);

    }`


here is the POJO

`@XmlRootElement(name = "countryLinks")
public class CountryLinks  {

    @XmlElement(name = "countryLink")
    private List<Link> countries = null;

    public CountryLinks() {
        countries = new ArrayList<Link>();
    }

    public void addCountry(Link country) {
        countries.add(country);
    }

    public List<Link> getCountries() {
        return countries;
    }

    public void setCountries(List<Link> countries) {
        this.countries = countries;
    }
}

响应应该如下所示

<media:countryLinks xmlns:media="urn:media.services" xmlns:xl="http://www.w3.org/1999/xlink">
<countryLink xl:href="http://localhost:8080/Media/service/US" xl:type="locator">US</countryLink>
<countryLink xl:href="http://localhost:8080/Media/service/CA" xl:type="locator">CA</countryLink>
</media:countryLinks>

当我转换为字符串时,它看起来像

<countryLinks>
<countries>
<href>http://localhost:8080/Media/service/US</href>
<type>locator</type>
<val>US</val>
</countries>
<countries>
<href>http://localhost:8080/Media/service/CA</href>
<type>locator</type>
<val>CA</val>
</countries>
</countryLinks>

这是我的servlet.xml


    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="defaultContentType" value="application/xml" />
    </bean>
    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />


    <!--    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">-->



<!--    <bean id="xmlConverter" class="org.springframework.http.converter.xml.AbstractJaxb2HttpMessageConverter">-->
<!--        <property name="supportedMediaTypes">-->
<!--            <list>-->
<!--                <bean class="org.springframework.http.MediaType">-->
<!--                    <constructor-arg index="0" value="text" />-->
<!--                    <constructor-arg index="1" value="xml" />-->
<!--                    <constructor-arg index="2" value="UTF-8" />-->
<!--                </bean>-->
<!--            </list>-->
<!--        </property>-->
<!--    </bean>-->
    
    <!-- Exception Resolver -->
<!--    <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"/>-->
<!--    -->
<!--    &lt;!&ndash; Configure bean to convert JSON to POJO and vice versa &ndash;&gt;-->
<!--    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!--        <property name="supportedMediaTypes" value="application/json" />-->
<!--    </bean>-->

    <bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/xml" />
    </bean>


</beans>

我不确定为什么这不起作用。有人知道我可以做什么来返回响应中的 xml pojo 吗?

java spring spring-mvc java-11 spring5
1个回答
0
投票

当服务器无法满足客户端的请求时,就会出现 HTTP 406 Not Acceptable 错误,因为所请求的资源在客户端请求中指定的可接受的内容类型中不可用。要解决此问题,客户端应调整请求中的 Accept 标头。

您需要为客户端添加标头请求 - 接受:application/xml

© www.soinside.com 2019 - 2024. All rights reserved.