简体   繁体   English

我正在尝试使Restful Webservice及其客户端成为传递XML时遇到的问题

[英]I am trying to make Restful Webservice and it's client,facing issue while passing XML

I am new to Restful WS.I am trying to make basic web-service.I am unable to pass XML content (I am able to pass when I change it to plain text successfully) 我是Restful WS的新手,我正在尝试提供基本的Web服务,我无法传递XML内容(成功将其更改为纯文本时,我可以通过)

package com.controller;

import javax.validation.constraints.NotBlank;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.pojo.Student;    

@RestController
public class Controllers {


    @RequestMapping(value="/hi",method=RequestMethod.POST,consumes=MediaType.TEXT_PLAIN_VALUE,produces=MediaType.APPLICATION_XML_VALUE)
    public String hello(@RequestBody String std) {


        System.out.println(std);

        return "Response";
}
}




    package com.pojo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {



@XmlElement(name="str")
private String str;

public String getStr() {
    return str;
}

public void setStr(String str) {
    this.str = str;
}
}

Client- 客户-

package com.me.app;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

import com.pojo.Student;

@SpringBootApplication
@ComponentScan(basePackageClasses=com.controller.Controllers.class)
public class App {





    public static void main(String args[])
    {


        SpringApplication.run(App.class, args);
        getEmployees();
    }

    private static void getEmployees()
    {
        System.out.println("starting");
        final String uri = "http://localhost:8005/hi";
        Student std = new Student();
        std.setStr("Something");
        String s=jaxbObjectToXML(std);
        System.out.println(s);
        RestTemplate restTemplate = new RestTemplate();

        String result = restTemplate.postForObject(uri, s, String.class);

        System.out.println(result);
    }
    private static String jaxbObjectToXML(Student std) {
        String xmlString = "";
        try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Marshaller m = context.createMarshaller();

            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML

            StringWriter sw = new StringWriter();
            m.marshal(std, sw);
            xmlString = sw.toString();

        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return xmlString;
    }

}

Can anyone help me in passing content in XML.In the above scenario,I am able to pass text as content and its working fine. 任何人都可以帮助我传递XML格式的内容。在上述情况下,我能够将文本作为内容传递,并且可以正常工作。

Thanks in advance! 提前致谢!

Try adding m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 尝试添加m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Jersey创建Restful WebService时出错 - Errors while trying to make a Restful WebService with Jersey 尝试在“ Windows 8.1”操作系统中更新“ Java”时遇到问题 - I am facing issue while trying to update “Java” in “windows 8.1” OS 在尝试在eclipse插件开发中检测用户的选择时,我遇到错误“ NullPointExeption” - I am facing error “NullPointExeption”, while trying to detect the user's selection in the eclipse plugin development 运行我的应用程序时,我在 Flutter 中遇到问题 - I am facing issue in Flutter while run my application 我在点击图片海报时遇到ElementNotVisible问题 - I am facing ElementNotVisible issue while clicking on image poster 在RestFul Web服务中传递JSONObject - Passing JSONObject in RestFul Webservice RESTful Web服务生成XML - XML produce by RESTful webservice XML RESTful问题的最简单的Jersey客户端 - Simplest Jersey Client for xml RESTful issue 尽管传递了预期的参数,为什么我仍然面临 junit 5 assertThrows 断言 TimedOutException 的问题? - Why am I facing issue with junit 5 assertThrows for asserting TimedOutException despite passing expected parameters? XPath解析xml时遇到问题 - facing issue while parsing xml with xpath compile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM