java - 使用restful web服务代替soap,而不改变请求的用户部分

标签 java spring rest web-services soap

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

SOAP 要求:

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

这是我的休息 Controller ,它应该能够响应来自 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 请求并将其存储在字符串缓冲区中。我知道如果可以使用休息来处理 SOAP 请求,那么这不是应该完成的方式。我知道休息是一种架构,与 SOAP 有很大不同,而且看起来至少没有直接的方法可以使用休息来处理 SOAP 请求。

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

最佳答案

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

SOAP 协议(protocol)是一种传递包含响应和调用元数据的信息的方法。

在休息时,您可以在响应正文中获得答案,最常用的格式是 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
    }
}

如有任何问题,请随时提出。

关于java - 使用restful web服务代替soap,而不改变请求的用户部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61346553/

相关文章:

java - 根据文件调用实例方法 updateValues

Spring 事务管理器 : Rollback doesnt work

java - 请放心/Stack Exchange API : Unable to get success response while trying to hit service from Eclipse

javascript - 为什么这个来自 Mozilla 的 XMLHttpRequest 示例在 Firefox 3 中不起作用?

java - 模型属性存储在哪里?

java - 比 JODCONVERTER 更快

java - addShutdownHook 和 setUncaughtExceptionHandler 在 java 中无法按预期工作

spring - Spring中 "constructor based injection"和 "autowire by constructor mode"有什么区别

java - 缓存条目不会从缓存存储 (SingleFileCacheStore) 中删除

java - 如何将 JSONObject 发送到 REST 服务?