简体   繁体   English

无法从Java中的CXF REST Web服务读取或解析纯字符串响应

[英]Can't read or parse Plain String Response from CXF REST Web Service in Java

A REST Service is implemented correctly in SpringMVC, deployed, and returns the correct result string, REST服务已在SpringMVC中正确实现,部署并返回正确的结果字符串,

@RestController
public class RESTServiceController {

    @GET
    @Produces("application/json") 
    @RequestMapping("/restUrl")
    public String getResult() {
        JSONObject json = new JSONObject();
        json.put("result", Boolean.TRUE);
    }
}

When testing this Web Service I get the correct output, 测试此Web服务时,我得到正确的输出,

{"result":true}

The problem is the caller gets the Response object via CXF, but I don't know how to parse the Response. 问题是调用者通过CXF获取Response对象,但是我不知道如何解析Response。 I don't need a custom object. 我不需要自定义对象。 All I need is the direct string, I just want to see my output string. 我需要的只是直接字符串,我只想查看我的输出字符串。

String restServiceURI = "http://www.asite.com/restUrl";
WebClient client = WebClient.create(restServiceURI,true);
Response resp = client.get();
//String entity = (String)resp.getEntity; <-- Also tried this to cast to a string

The issue is, the Response length is 0, Status is 302. The getEntity InputStream brings back an EmptyInputStream based on what the debugger shows. 问题是,响应长度为0,状态为EmptyInputStream getEntity InputStream根据调试器显示的内容带回EmptyInputStream

The Response object doesn't have any info that I can see in the debugger. Response对象没有我可以在调试器中看到的任何信息。

How do I just get my direct string back? 我怎样才能找回我的直接字符串? Is there an example? 有例子吗?

You are trying mix both Spring Rest and CxF rest. 您正在尝试混合使用Spring Rest和CxF rest。 Either use Spring Rest or CXF Rest. 使用Spring Rest或CXF Rest。

If you want to use Spring Rest as shown below 如果您想使用Spring Rest,如下所示

@Service
public class RESTServiceController {

    @RequestMapping("/restUrl")
    public @ResponseBody MyClass getResult() {
        return myClass;
    }
}

or CXF as shown below. 或CXF,如下所示。

@Service
public class RESTServiceController {

    @GET
    @Produces("application/json") 
    public MyClass getResult() {
        return myClass;
    }
}

Note: You need not use json conversion explicitly, both Spring Rest and CXF has to feature to convert your object to json string. 注意:您无需显式使用json转换,Spring Rest和CXF都必须具有将对象转换为json字符串的功能。

However your issue doesn't stop here, I believe you've enabled spring-security, which is sending redirect(302) response, with login page. 但是,您的问题并不仅限于此,我相信您已启用spring-security,该安全性通过登录页面发送redirect(302)响应。 You can verify response from server by enabling logging in client side. 您可以通过启用客户端登录来验证来自服务器的响应。

        WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
        WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM