如果我的格式关闭(这里是新的),请原谅。我有一个如下所示的 REST 服务(省略了实现细节):
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
@Controller
@RequestMapping(value={"/myservice", "/myservice/"}, method=RequestMethod.POST)
public class MyClass{
@RequestMapping(value={"/",""}, method=RequestMethod.GET)
public ModelAndView doSomething(@RequestParam(value="params", required=true) String params){
Map<String,Object> mymap = new HashMap<String,Object>();
mymap.put("myparam",params);
return new ModelAndView(new MappingJacksonJsonView(), mymap);
}
}
我想要做的是编写一个 HttpClient(使用 org.apache.commons.httpclient.HttpClient,我知道那里有另一个来自 apache 的 httpclient),它可以从上述服务中获取“mymap”对象。我知道我必须在我的客户端代码中执行以下操作:
public Map<String,String> getMap(){
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("myurl");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
NameValuePair [] pair = { new NameValuePair("content","mytestcontentvalue")};
((GetMethod)method).setQueryString(pair);
int code = client.executeMethod(method);
Map<String,String> mymap = ?? /// what do i do here?
return mymap;
}
我一直在寻找解决方案,但在这里找不到阅读回复的方法。这是我第一次编写客户端和服务,所以我可能找到了解决方案,但从未理解它:(任何建议都会有帮助!!!谢谢。