一、通过地址栏传值
1、项目结构
2、pom.xml
4.0.0
test.com
test-com
war
1.0-SNAPSHOT
test-com Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-test
${spring.version}
javax.servlet
jstl
1.2
org.springframework
spring-aspects
3.0.6.RELEASE
org.aspectj
aspectjrt
1.6.11
org.aspectj
aspectjweaver
1.6.11
cglib
cglib
2.1_3
javax.servlet
servlet-api
2.5
provided
net.sf.json-lib
json-lib
2.4
jdk15
test-com
UTF-8
3.1.2.RELEASE
3、web.xml
/p>
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application
dispatcher
org.springframework.web.servlet.DispatcherServlet
1
dispatcher
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
4、dispatcher-servlet.xml
default-lazy-init="true">
5、TestController.java
packagecom.controller;importcom.log.Log;importcom.remote.RemoteDemo;importnet.sf.json.JSONObject;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;/*** Created by sunjf on 2016/6/26.*/@Controllerpublic classTestController {
@Log(desc= "log测试")
@RequestMapping(value= "test1")publicString test1(){
String result= RemoteDemo.remoteJsonRequest("http://localhost:8080/test2?username=sun&password=123", 5000, newJSONObject());
System.out.println(result);return "success";
}
}
6、RemoteDemo.java
packagecom.remote;importnet.sf.json.JSONObject;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.URL;/*** Created by Administrator on 2016/6/27.*/
public classRemoteDemo {public static String remoteJsonRequest(String url, inttimeout, JSONObject object) {
URL connect;
StringBuffer data= newStringBuffer();try{
connect= newURL(url);
HttpURLConnection connection=(HttpURLConnection) connect.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setReadTimeout(timeout);
connection.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
paramout.write(object.toString());
paramout.flush();
InputStream inputStream =connection.getInputStream();
InputStreamReader inputStreamReader= new InputStreamReader(inputStream, "UTF-8");
BufferedReader reader= newBufferedReader(inputStreamReader);
String line;while ((line = reader.readLine()) != null) {
data.append(line);
}
paramout.close();
reader.close();
}catch(Exception e) {
e.printStackTrace();
}returndata.toString();
}
}
7、调用另一项目中的借口(TestController.java)
packagecom.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;/*** Created by sunjf on 2016/6/26.*/@Controllerpublic classTestController {
@RequestMapping(value= "test2")
@ResponseBodypublicString test2(String username, String password){
System.out.println(username+ " | " +password);return username + " | " +password;
}
}
8、index.jsp
Hello World!
9、success.jsp
Hello Success!
二、同过jsonObject传值,通过object接收
1、TestController.java,请求端发送请求
packagecom.controller;importcom.log.Log;importcom.remote.RemoteDemo;importnet.sf.json.JSONObject;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;/*** Created by sunjf on 2016/6/26.*/@Controllerpublic classTestController {
@Log(desc= "log测试")
@RequestMapping(value= "test1")publicString test1(){
JSONObject jsonObject= newJSONObject();
jsonObject.put("username", "sun");
jsonObject.put("password", "123");
String result= RemoteDemo.remoteJsonRequest("http://localhost:8080/test2", 5000, jsonObject);
System.out.println(result);return "success";
}
}
2、TestController.java,被访问端接口
packagecom.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.ResponseBody;/*** Created by sunjf on 2016/6/26.*/@Controllerpublic classTestController {
@RequestMapping(value= "test2", method = RequestMethod.POST, produces="application/json")
@ResponseBodypublicString test2(@RequestBody User user){
System.out.println(user.toString());returnuser.toString();
}
}
注:注意此时的@RequestBody注解,为此需要在被访问端引入依赖
org.codehaus.jackson
jackson-mapper-asl
1.9.8
jar
compile
3、User.java,接收实体类
packagecom.controller;/*** Created by Administrator on 2016/6/28.*/
public classUser {privateString username;privateString password;
@OverridepublicString toString() {return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}
}