我有一些非常基本的代码来调用 Web 服务。使用 RestTemplate().getForObject 会给我“不是有效的 HTTP URL”错误。
Exception in thread "main" java.lang.IllegalArgumentException: [localhost:9080/someApp/testWs/partners/1] is not a valid HTTP URL
at org.springframework.web.util.UriUtils.encodeHttpUrl(UriUtils.java:245)
at org.springframework.web.client.RestTemplate$HttpUrlTemplate.encodeUri(RestTemplate.java:651)
at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:140)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:400)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
at org.kofc.test.TestTemplate.main(TestTemplate.java:26)
这是客户端代码:
package org.smpkg.test;
import org.smpkg.dvo.Prtnr;
import org.springframework.web.client.RestTemplate;
public class TestTemplate
{
private static final long serialVersionUID = 1130201273334264152L;
public static void main(String[] args){
System.out.println("I am here ....");
Prtnr partner = new RestTemplate().getForObject("localhost:9080/someApp/testWs/partners/{id}", Prtnr.class, "1");
System.out.println("partner name is : " + partner.getPrtnrNm());
}
}
控制器
@RequestMapping(value="/{id}", method=RequestMethod.GET)
@ResponseBody
public Prtnr getPartner(@PathVariable("id") String id){
....
....
return partner;
}
servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="org.somepkg.bso" />
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
<bean id="content" class="com.springsource.samples.rest.SampleContentAtomView"/>
</beans>
访问链接 localhost:9080/someApp/testWs/partners/1> 返回结果,但使用 RestTemplate().getForObject() 不起作用。
这是 Prtnr 类:
public class Prtnr implements Cloneable, java.io.Serializable {
private static final long serialVersionUID = 201207021420600052L;
private Integer prtnrId;
private String creatUserId;
private Date creatTs;
private String updtUserId;
private Date updtTs;
private String prtnrNm;
private Integer cncilNum;
private Character prtnrTypCd;
private Set<PrtnrGeoInfo> prtnrGeoInfos = new HashSet<PrtnrGeoInfo>(0);
private Set<PrtnrDtl> prtnrDtls = new HashSet<PrtnrDtl>(0);
private Set<SuplyDtl> suplyDtls = new HashSet<SuplyDtl>(0);
private Set<TrnsprtDtl> trnsprtDtls = new HashSet<TrnsprtDtl>(0);
private Set<PrtnrFacil> prtnrFacils = new HashSet<PrtnrFacil>(0);
private Set<PrtnrHumanResrc> prtnrHumanResrcs = new HashSet<PrtnrHumanResrc>(0);
.....
.....
Getters and setters for these properties
...
}
我在服务器端看到的错误是:[12/3/12 10:25:57:764 EST] 00000039 ServletWrappe E SRVE0068E:在 servlet 的服务方法之一中抛出未捕获的异常:
spitter.
Exception thrown : org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:
org.kofc.dvo.Prtnr["prtnrGeoInfos"]->org.hibernate.collection.PersistentSet[0]->org.kofc.dvo.PrtnrGeoInfo["id"]->
org.kofc.dvo.PrtnrGeoInfoId["partner"]->
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:164)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
Web 服务的卷曲是: org.codehaus.jackson.map.deser 的 org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:579) 的线程“main”java.lang.StackOverflowError 中的异常。 SettableBeanProperty.deserialize(SettableBeanProperty.java:299) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer. java:697) 在 org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
我错过了什么???
谢谢