如何在xml中表示响应主体

时间:2015-11-09 07:04:18

标签: java xml spring-mvc response

您好我正在使用spring框架,默认表示形式是JSON。我试图以3种格式表示响应主体。 Xml,json和html。默认方式是json,但在xml和html中表示有问题。你希望响应体如何在xml和html中。运行代码时出现406错误。

在我的pom xml中:我声明了

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>

     <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.6.3</version>
    </dependency>

并在我的dispatcher-servlet.xml中:

       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
       <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>

最后我的控制器方法:

@RequestMapping(value="person/{id}", method=RequestMethod.GET, params="format=xml", produces=MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public ResponseEntity<?> PersoninXML(@PathVariable("id")Long id) {
        Person person = personDAO.findById(id);
        if(person!=null){
            return new ResponseEntity<Person>(person, HttpStatus.OK);
        }
        else
            return new ResponseEntity<String>("Id doesn't exist", HttpStatus.NOT_FOUND);

    }

0 个答案:

没有答案