在我的应用程序中,我在触发代码
RestTemplate client=new RestTemplate();
if(ipaddress.length>0)
{
for (int i = 0; i < ipaddress.length; i++) {
System.out.println(baseUrl+"/grid/api/proxy?id="+ipaddress[i]);
ResponseEntity<Object> object= client.getForEntity(baseUrl+"/grid/api/proxy?id="+ipaddress[i], Object.class);
System.out.println(object);
}
}
结果对象返回如下所示:
<200 OK,{success=true,
msg=proxy found !,
id=http://192.168.3.37:5555,
request={class=org.openqa.grid.common.RegistrationRequest,
id=null,
name=null,
description=null,
configuration={port=5555,
servlets=[],
host=192.168.3.37,
Dwebdriver.ie.driver=C:\Anoop\IEdriver\IEDriverServer.exe=,
cleanUpCycle=5000,
Dwebdriver.chrome.driver=C:\Anoop\chromeDriver\chromedriver.exe=,
browserTimeout=0,
hubHost=localhost,
registerCycle=5000,
capabilityMatcher=org.openqa.grid.internal.utils.DefaultCapabilityMatcher,
newSessionWaitTimeout=-1,
hub=http://localhost:4444/grid/register,
url=http://192.168.3.37:5555,
remoteHost=http://192.168.3.37:5555,
register=true,
throwOnCapabilityNotPresent=true,
nodePolling=5000,
proxy=org.openqa.grid.selenium.proxy.DefaultRemoteProxy,
browser=browserName=firefox,
maxSession=5,
role=node,
jettyMaxThreads=-1,
hubPort=4444,
timeout=300000},
capabilities=[{seleniumProtocol=WebDriver,
platform=WIN8_1,
browserName=firefox},
{seleniumProtocol=WebDriver,
platform=WIN8_1,
browserName=chrome},
{seleniumProtocol=WebDriver,
platform=WIN8_1,
browserName=internet explorer}]}},
{Content-Type=[application/json;charset=UTF-8],
Content-Length=[1185],
Server=[Jetty(7.x.y-SNAPSHOT)]}>
如果我在响应实体方法中使用JSONObject
而不是Object
,我会得到{}
如何将Object
转换为JSONObject
?
注意:我的应用程序不是Web应用程序。
答案 0 :(得分:0)
您可以在配置RestTemplate时在application-context.xml中使用以下代码。
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory" />
<property name="messageConverters">
<list>
<beans:bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.FormHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<beans:bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</beans:bean>
<ref bean="jsonConverter" />
</list>
</property>
</beans:bean>
<beans:bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</beans:bean>
<beans:bean id="annotationMethodHandlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</beans:bean>
希望这些东西可以帮助你。如果有任何疑问,请告诉我。
答案 1 :(得分:0)
ResponseEntity<Object> object= client.getForEntity(baseUrl+"/grid/api/proxy?id="+ipaddress[i], Object.class);
在这里你可以传递你想要的对象,我建议如果你知道返回的对象结构来创建一个虚拟mixin类,HttpMessage转换器将负责将它转换为你的虚拟类。如果您希望其他JSON提供程序负责反序列化,您可以在ResponseEntity<T>
中传递String并手动反序列化。
样本用法:
ResponseEntity<Identifier> postForEntity = postForEntity = getTemplate().postForEntity(builder.toUri(), entity, Identifier.class);
logger.info("creating creative result : "+postForEntity.getBody());