java 如何在发出 RESTful 请求时使用 Spring 3.0 mvc 将 XML 转换为对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1690618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:37:11  来源:igfitidea点击:

How can I Convert XML to an Object using Spring 3.0 mvc while making RESTful request

javaspringspring-mvcrest

提问by brock

I'm using the Spring 3.0 RC1 framework and I'm currently testing out Spring mvc. I wanted to use Spring mvc to handle restful requests. I have set up my controller to handle the URI request. I am passing in xml with the request. So on the controller I have a method like follows:

我正在使用 Spring 3.0 RC1 框架,目前正在测试 Spring mvc。我想使用 Spring mvc 来处理安静的请求。我已经设置了我的控制器来处理 URI 请求。我正在通过请求传入 xml。所以在控制器上我有一个如下的方法:

public void request(RequestObject request) {
  doSomething();
}

I am having a hard time converting the xml to the RequestObject. I haven't seen much documentation on this and I was wondering if anyone could point me in the right direction. I'm guess that you would have to annotate the RequestObject using JAXB or something in order to tell Spring to convert the xml file to RequestObject but I'm not sure.

我很难将 xml 转换为 RequestObject。我没有看到太多关于此的文档,我想知道是否有人可以指出我正确的方向。我猜你必须使用 JAXB 或其他东西来注释 RequestObject 才能告诉 Spring 将 xml 文件转换为 RequestObject 但我不确定。

Thanks for all of your help!!

感谢您所有的帮助!!

采纳答案by Yevhen

For converting XML to Java object you can use Apache Digest http://commons.apache.org/digester/. Spring uses it itself internally.

要将 XML 转换为 Java 对象,您可以使用 Apache Digest http://commons.apache.org/digester/。Spring 在内部使用它本身。

UpdateI wasn't aware about this new feature in Spring 3.0. Sorry for misdealing you. I wrote quick test and this is what you should do.

更新我不知道 Spring 3.0 中的这个新特性。对不起,误会了你。我写了快速测试,这是你应该做的。

1) Set up ViewResoler and MessageConverter in -servlet.xml. In my test it looks like this

1) 在-servlet.xml 中设置ViewResoler 和MessageConverter。在我的测试中它看起来像这样

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <bean id="person" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <property name="contentType" value="application/xml"/>
        <property name="marshaller" ref="marshaller"/>
    </bean>

    <oxm:jaxb2-marshaller id="marshaller">
        <oxm:class-to-be-bound name="com.solotionsspring.test.rest.model.Person"/>
    </oxm:jaxb2-marshaller>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="marshallingHttpMessageConverter"/>
          </list>
        </property>
    </bean>

    <bean id="marshallingHttpMessageConverter" 
          class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
      <property name="marshaller" ref="marshaller" />
      <property name="unmarshaller" ref="marshaller" />
    </bean>

2) Add XML structure annotations into your Java class

2) 将 XML 结构注释添加到您的 Java 类中


@XmlRootElement
public class Person {
    private String name;
    private int age;
    private String address;
    /**
     * @return the name
     */
    @XmlElement
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    @XmlElement
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the address
     */
    @XmlElement
    public String getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
}

3) Add mapping annotation into your Controller class like

3)将映射注释添加到您的控制器类中,例如


@Controller
public class RestController {

    @RequestMapping(value = "/person", method = RequestMethod.PUT)
    public ModelMap addPerson(@RequestBody Person newPerson) {
        System.out.println("new person: " + newPerson);
        return new ModelMap(newPerson);
    }    
}

Hope this will help you.

希望这会帮助你。