Spring MVC @Response Body XML有额外的<string>标签 - 为什么?</string>

时间:2014-11-17 21:39:44

标签: java xml spring spring-mvc

我配置了如下所示的内容:

@Configuration
@EnableWebMvc
@ComponentScan(value = "com.whatever.api")
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(createXmlHttpMessageConverter());
        converters.add(new MappingJackson2HttpMessageConverter());

        super.configureMessageConverters(converters);
    }

    private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
        MarshallingHttpMessageConverter xmlConverter =
            new MarshallingHttpMessageConverter();

        // List<MediaType> mediaType = new ArrayList<>();
        // mediaType.add(MediaType.TEXT_XML);

        XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
        // xmlConverter.setSupportedMediaTypes(mediaType);
        xmlConverter.setMarshaller(xstreamMarshaller);
        xmlConverter.setUnmarshaller(xstreamMarshaller);

        return xmlConverter;
    }

    @Bean
    MappingJackson2HttpMessageConverter jacksonConverter() {
        return new MappingJackson2HttpMessageConverter();
    }

    @Bean
    MarshallingHttpMessageConverter xmlConverter() {
        return new MarshallingHttpMessageConverter();
    }

    @Bean
    XStreamMarshaller xstreamMarshaller() {
        return new XStreamMarshaller();
    }

    // added to support files in body of add document request
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        return multipartResolver;
    }
}

这是返回响应中的XML的方法:

    // @RequestMapping(value = "/getDocuments", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
@RequestMapping(value = "/getDocuments", method = RequestMethod.GET)
@ResponseBody
public String
statementList(@RequestParam("doctype") String doctype,
    @RequestParam("bin") String bin,
    @RequestParam("br") String br,
    @RequestParam("mid") String mid,
    @RequestParam("passwd") String passwd,
    @RequestParam("dates") String dates,
    HttpServletRequest request) {

    String xmlResponse;
    RequestHandler requestHandler = new RequestHandlerImpl(systemProperties, null, tableDataDao);
    xmlResponse = requestHandler.getStatements(bin, br, mid, passwd, dates);
    System.out.println("xmlResponse=" + xmlResponse);

    return xmlResponse;
}

xmlResponse String值是正确的XML,与我想要的完全一样。我的问题是响应最终看起来像这样:

    <string><?xml version="1.0" encoding="UTF-8"?><a bunch of correct, but unformatted, XML here></string>

明显的困难是似乎来自无处的“”标签。从我已经注释掉的内容可以看出,我试图设置MediaType(到TEXT_PLAIN,TEXT_XML和APPLICATION_XML),无论我选择什么,它都不会摆脱无关的标签。

响应中的XML正确非常重要,就像在xmlResponse String值中一样。有没有我缺少的设置或什么?

2 个答案:

答案 0 :(得分:1)

您好像是在String中将响应转换为XML requestHandler。但是,requestHandler应该返回一个业务对象,而statementList应该返回该对象,而不是String

请参阅@RequestMapping Javadocs

  

处理程序方法支持以下返回类型:

     
      
  • @ResponseBody带注释的方法(仅限Servlet),用于访问Servlet响应HTTP内容。返回值将使用message converters转换为响应流。
  •   

由于statementList返回String,邮件转换器会将其转换为XML(这就是您在<string>...</string>中看到回复的原因。)

答案 1 :(得分:0)

在kuporific的帮助下,我解决了这个问题。

我必须添加一个转换器,如下所示:

@Configuration
@EnableWebMvc
@ComponentScan(value = "com.roler.api")
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
    }

    @Bean
    MappingJackson2HttpMessageConverter jacksonConverter() {
        return new MappingJackson2HttpMessageConverter();
    }

    // added to support plain text Spring responses
    @Bean
    StringHttpMessageConverter stringHttpMessageConverter() {
        return new StringHttpMessageConverter();
    }
...

然后我必须在这里指定纯文本:

@RequestMapping(value = "/getDocuments", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody public String statementList(...)

...它可以在不需要HttpServletResponse对象的情况下工作。

再次感谢您的帮助,希望有人能发现这有用。