问题描述
好吧,我有一个简单的REST API,它现在正在运行。我也有一个移动应用(Android),将要准备一些网络通信。但这里的东西:
Okay I'm having a simple REST API that is now running. I also got a mobile app (Android) that would be ready for some network communication. But here is the thing:
我现在得到了与的春天,我可以以http-请求使用花哨的API:
I now got a fancy API created with Spring that I can use with Http-Requests:
package com.helloworld
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("msg", "JCG Hello World!");
return "helloWorld";
}
}
这是所有罚款,但我怎么会用在我的Android手机应用这个API?我的意思是,我不知道如何让这些调用更优雅和简单!我不想写我的API是这样的每个请求:
This is all fine but how would I use this API in my Android mobile App? What I mean is that I don't know how to make those calls more elegantly and simple! I don't want to write for every request on my API something like this:
class HelloWorldTask extends AsyncTask<String, String, String> {
private String url = "http://192.168.42.229:8080/HelloWorldExample/helloWorld/displayMessage/" + msg;
@Override
protected String doInBackground(String... uri) {
String msg = "";
for(String m : uri) { msg += m; }
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate.getForObject(url, String.class, "Android");
}
}
有什么办法避免这种情况?说实话,我相信,我将能够只导入有点像的HelloWorld-api.jar文件
建设我的REST API的项目,然后做这样的事情后:
Is there any way to avoid this? To be honest I believed that I would be able to just import something like a helloworld-api.jar
after building my REST API project and then do something like this:
import com.helloworld
public void getServerHello() {
HelloWorldController api;
HelloWorld helloWorld = api.getHelloWorldMapping();
helloWorld.displayMessage("How are you?");
}
和,这将是它。所以写一个Spring REST API或我真的要使用像一个URI的建设者和创造各种请求的时候,有没有办法做这样的事情一个额外的类&LT;凡是&GT;任务延伸AsyncTask的&LT;,&GT;?
由我自己?
And that this will be it. So is there a way to do something like this when writing a Spring REST API or will I really have to use something like a URI-builder and create for all kind of requests an additional class <whatever>Task extends AsyncTask<?, ?, ?>
by myself?
由于API是明确规定它应该可以做这样的事情,但果真如此吗?
Since the API is clearly defined it should be possible to do something like this, but is it?
我希望我的问题是明确的 - 请让我知道如果不是
I hope my question is clear - please let me know if not.
推荐答案
在我目前的项目中,我们有一个Java客户端和基于泉REST API一个RESTful后端类似的设置。我们使用Apache的CXF-RS-客户端API从客户拨打我们的REST服务。
In my current project we have similar setting with a Java client and a RESTful backend based on Springs REST API. We use apache-cxf-rs-client-api to call our REST services from the clients.
1)在你的HelloWorldController你的方法创建一个接口
1) Create an Interface for your Methods in your HelloWorldController
2)Apache的CXF-RS-客户端API添加到您的项目(看看更多的信息:的)
2) add apache-cxf-rs-client-api to your project (take a look for more informations: https://cxf.apache.org/docs/jax-rs-client-api.html)
3)现在你可以调用客户端的接口和Apache CXF将完成剩下的给你。
3) now you can call the interface on the client and Apache CXF will do the rest for you.
这篇关于使用Spring简单的REST API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!