有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring中从响应体生成xml的java错误

大家好,我有点问题。我使用spring框架编写了一个简单的rest应用程序。我能够用json发布和获取响应,但现在我希望能够同时显示json和xml格式。现在我在尝试以xml显示格式时遇到错误406。我已经在dispatcher中包含了转换器,但在以xml格式显示时遇到了问题。求你了,我很想念你。还包括maven中的依赖项:

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

下面是我的调度程序servlet。加载控制器的xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc-annotation="http://www.springframework.org/schema/mvc"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:component-scan base-package="edu.sjsu.cmpe275.lab2.controller" />


       <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>
    <mvc:annotation-driven/>
</beans>

以下是我在尝试以xml生成输出时输入url时在控制器中的方法:

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

    }

个人POJO:

public class Person {
    @Id
    @Column(name="person_Id")
    @GeneratedValue
    private long id;
    @Column(unique = true)
    private String email;
    private String first_name, last_name, description;

    //@Embedded
    private Address address;
    @ManyToOne(cascade = CascadeType.ALL)
    private Organization organization;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name="Friends")
    Collection<Person> persons = new ArrayList<Person>();

    public Organization getOrganization() {
      return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Person() {}


    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

共 (0) 个答案