簡體   English   中英

我正在嘗試使Restful Webservice及其客戶端成為傳遞XML時遇到的問題

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

我是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;
}
}

客戶-

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;
    }

}

任何人都可以幫助我傳遞XML格式的內容。在上述情況下,我能夠將文本作為內容傳遞,並且可以正常工作。

提前致謝!

嘗試添加m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM