Rest Webservice 環境搭建流程和例項
1 pojo
package com.train.pojo;
public classRestResponse {
/**
* 返回碼
*/
privateintresponseCode;
/**
* 資料
*/
privateObject data;
/**
* 簡介
*/
privateString Message;
publicintgetResponseCode() {
return responseCode;
}
publicvoidsetResponseCode(intresponseCode) {
this.responseCode = responseCode;
}
publicObject getData() {
return data;
}
publicvoidsetData(Object data) {
this.data = data;
}
publicString getMessage() {
return Message;
}
publicvoidsetMessage(String message) {
Message = message;
}
@Override
publicString toString() {
return "RestResponse [responseCode="+ responseCode+ ", data="+ data
+ ", Message=" + Message + "]";
}
}
1 client
package com.train.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import com.train.pojo.RestResponse;
@Controller
@RequestMapping("/send")
public classSendToServiceController {
protectedRestTemplate restTemplate= newRestTemplate();
@RequestMapping("/get")
publicvoidgetTest() {
String path="http://localhost:8080/train_010_spring_springmvc_rest_webservice/hello/get?value={value}";
String p1="getname";
RestResponse response = restTemplate.getForObject(path,RestResponse.class,p1);
System.out.println(response.getResponseCode());
System.out.println(response.getData());
}
@RequestMapping("/post")
publicvoidpostTest() {
//類似,網上例子多
}
}
2 service
package com.train.web.v1.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.train.pojo.RestResponse;
@Controller
@RequestMapping("/hello")
public classHelloWebServiceController {
@RequestMapping("/get")
@ResponseBody
publicRestResponse getTest(HttpServletRequest request,Model model){
String data = "";
Object value = request.getParameter("value");
if(value!=null &&value.toString().equals("getname")){
data= "大家好";
}
RestResponse response = new RestResponse();
if(!data.equals("")){
response.setResponseCode(0);
response.setMessage("SUCCESS");
}
else {
response.setResponseCode(999);
response.setMessage("Error occured");
}
response.setData(data);
return response;
}
@RequestMapping("/post")
publicvoidpostTest(){
}
}
3 環境搭建,就是普通的spring 環境另外加上兩個jar包
jackson-core-asl-1.9.13 jackson-mapper-asl-1.9.13
相關文章
- Spark程式設計環境搭建及WordCount例項Spark程式設計
- 以QT為例談環境搭建QT
- C#中WebService的建立、部署和呼叫的簡單例項C#Web單例
- wsl2-Ubuntu20.04_selenium2環境搭建流程Ubuntu
- Redis多例項及主從複製環境搭建Redis
- 環境搭建
- ORACLE-LINUX環境字元介面單例項安裝OracleLinux字元單例
- Ubuntu 18.04.1 搭建Java環境和HelloWorldUbuntuJava
- Elasticsearch快速入門和環境搭建Elasticsearch
- ZooKeeper 系列(二)—— Zookeeper單機環境和叢集環境搭建
- Activiti的流程例項【ProcessInstance】與執行例項【Execution】
- 搭建lnmp環境LNMP
- 搭建gym環境
- Linuxg環境搭建Linux
- JDK環境搭建JDK
- Angular環境搭建Angular
- anaconda 環境搭建
- ReactNative環境搭建React
- swoft 環境搭建
- Flutter環境搭建Flutter
- 搭建Java環境Java
- Supervisor 環境搭建
- react環境搭建React
- FNA環境搭建
- FNA 環境搭建
- Maven 環境搭建Maven
- Dubbo環境搭建
- Vagrant 環境搭建
- LNMP 環境搭建LNMP
- OpenGL 環境搭建
- App環境搭建APP
- gogs環境搭建Go
- Kubernetes環境搭建
- mac搭建環境Mac
- python環境搭建Python
- keil環境搭建
- Python3 環境搭建(Windows和Linux)PythonWindowsLinux
- Windows環境下的Nginx環境搭建WindowsNginx
- window環境下testlink環境搭建(xammp)