fack_friend
165
2013-11-08 22:46:50
0
26821

post로 rest쏘기! (http body에 data담아서..)


2일걸려서 샘플같지도 않은 샘플만들었네요 ㅠㅠ
혹시 있을지 모르는 저와 같은 쌩초보분들을 위해 
자료 정리겸 공유하려고 올립니다...

rest방식의 post send 샘플예제입니다..

client side 
- json형태로 String값을 http-body에 담아서 URL로 넘깁니다..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import org.json.JSONException;
import org.json.JSONObject;


public class cxz {
public static void main(String[] args) throws JSONException, IOException {
JSONObject json = new JSONObject();
json.put("test", "test");
json.put("test1", "test1");
json.put("test2", "test2");
json.put("test3", "test3");
json.put("test4", "test3");
json.put("test5", "test3");
json.put("test6", "test3");
String body = json.toString();
System.out.println(body);
URL postUrl = new URL("http://localhost:8080/SpringMVC/movie/test");
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true); // xml내용을 전달하기 위해서 출력 스트림을 사용
connection.setInstanceFollowRedirects(false);  //Redirect처리 하지 않음
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os= connection.getOutputStream();
os.write(body.getBytes());
os.flush();
System.out.println("Location: " + connection.getHeaderField("Location"));
 BufferedReader br = new BufferedReader(new InputStreamReader(
 (connection.getInputStream())));
  
 String output;
 System.out.println("Output from Server .... \n");
 while ((output = br.readLine()) != null) {
 System.out.println(output);
 }
connection.disconnect();
//81page~
}
}


server-side
- http-body로 들어온 데이터를 받아서 model.addAttribute를 이용해 list.jsp로 보냅니다..

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


    @RequestMapping(value="/test",method = RequestMethod.POST)
    public String testpost(@RequestBody String body,ModelMap model) throws Exception
    {
    model.addAttribute("body",body);
    return "list";
    }
}

list.jsp

<html>
<body>
<h1>Spring 3 MVC REST web service</h1>

<h3>Movie Name : ${body}</h3>
</body>
</html>

web.xml

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
        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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.mkyong.common.controller" />

<mvc:annotation-driven />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>

0
  • 댓글 0

  • 로그인을 하시면 댓글을 등록할 수 있습니다.