【问题标题】:Using restful web service in place of soap without altering the user part of the request在不改变请求的用户部分的情况下使用 restful web 服务代替肥皂
【发布时间】:2020-04-21 14:56:55
【问题描述】:

我一直在尝试使用 restful 网络服务来代替传统的肥皂网络服务,而不改变请求的用户部分。我想知道这是否可以实现。以下是演示该问题的示例代码:

肥皂请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:sch="https://www.flopradalley.com/xml/school">
<soapenv:Header/>
<soapenv:Body>
  <sch:StudentDetailsRequest>
     <sch:name>Arun</sch:name>
  </sch:StudentDetailsRequest>
</soapenv:Body>
</soapenv:Envelope>

这是我的休息控制器,它应该能够响应来自 SoapUI 的请求:

@RestController
@RequestMapping(value = "/restservice")
public class KenRestController {

    @RequestMapping(value = "/details", method = RequestMethod.POST, 
    produces = MediaType.TEXT_XML_VALUE, consumes = MediaType.TEXT_XML_VALUE
    )
    public StudentDetailsResponse execute(HttpServletRequest request) 
    {
        StudentDetailsRequest objectFromBody = null;// unmarshall the object from soap request and store it here
        StringBuffer sb = new StringBuffer();

        try {
            BufferedReader reader = request.getReader();

            String lineStr = null;
            while ((lineStr = reader.readLine()) != null) {
            sb.append(lineStr);
        }catch(IOException ex) {
            ex.printStackTrace();
        }
        /* other code */
        return ---;
    }

}

我只能将soap请求作为文本获取并将其存储在字符串缓冲区中。我知道如果可以使用休息来处理肥皂请求,这不是远程应该完成的方式。我知道rest是一种架构,与soap有很大的不同,看起来至少没有直接的方法可以使用rest来处理soap请求。

除了这个示例代码,我应该能够从请求中提取 SoapMessage、MessageContext、Soapheader、SoapBody。这让我想到了最初的问题,这是否可能?

【问题讨论】:

    标签: java spring rest web-services soap


    【解决方案1】:

    如果您将更改更改为 REST API,我强烈建议您不要保留某些 Web 服务逻辑。

    SOAP 协议是一种传递信息的方法,其中包含呼叫的应答和元数据。

    在休息时,您会在响应正文中获得答案,最常用的格式是 json。可以直接将Spring REST解析成java对象的对象作为参数传入方法中(实际使用Jackson库解析)。

        @RestController
    public class KenRestController {
    
        @GetMapping("/restService/details/{studentId}") // post is for saving an elelement
        public StudentDetailsResponse getDetails(@PathVariable("studentId") Integer studentId) {
            // you get the details and parse it to the object you want to return
            return studentService.getStudentById(studentId);
        }
    
        // example of response class
        private class StudentDetailsResponse implements Serializable{
    
            // pojo class
        }
    }
    

    请不要犹豫,对此提出任何问题。

    【讨论】:

    • 我需要使用 Rest 处理一个肥皂请求。我认为这是不可能的。
    • 它们是两种不同的方法,它们的基础完全不同。我的建议是使用 web 服务或 REST,而不是混合使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2012-05-07
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多