[예제 프로젝트(maven)]
(데몬 실행)
maven clean compile jetty:run
[PPT 설명]
path를 받는 Annotation
<실행결과>
- Rest 방식과 기존 파라미터 요청 방식 을 같이 사용 가능??
사용가능하다.
실행결과
@Controller
@RequestMapping("/comm")
@ResponseStatus(value = HttpStatus.ACCEPTED)
public class JsonRequestController {
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value=HttpStatus.FOUND)
public void sendFruitMessage(@RequestBody Message name) {
System.out.println("name : " + name.getName());
return ;
}
}
@XmlRootElement(name = "message")
public class Message {
@XmlElement
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML Payload
<클라이언트>
$ curl -i -H "Content-Type: application/xml" -X get -d '<message><name>Kim Yong Hwan</name></message>' localhost:8080/comm
HTTP/1.1 302 Found
Content-Length: 0
Server: Jetty(6.1.26)
<서버>
name : Kim Yong Hwan
<클라이언트>
$ curl -i -H "Content-Type: application/json" -X get -d '{"name":"Kim Yong Hwan"}' localhost:8080/comm
HTTP/1.1 302 Found
Content-Length: 0
Server: Jetty(6.1.26)
<서버>
name : Kim Yong Hwan
* 실제 테스트 코드
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Rest Demo Application</display-name>
<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>
</web-app>
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.google.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
RestfulController.java
@Controller
@RequestMapping("/restful")
public class RestfulController {
@RequestMapping(value = "/message/{name}", method = RequestMethod.GET)
public String getMessage(@PathVariable String name, ModelMap model) {
model.addAttribute("message", name);
return "list";
}
@RequestMapping(value = "/command/{id}/content/{content}", method = RequestMethod.GET)
public String getCommand(@PathVariable("id") String id, @PathVariable("content") long content, ModelMap model) {
model.addAttribute("id", id);
model.addAttribute("content", content);
return "command";
}
@RequestMapping(value = "/link/{id}", method = RequestMethod.DELETE)
public String deleteLink(@PathVariable("id") String id, ModelMap model) {
model.addAttribute("id", id);
return "delete";
}
}
// WEB-INF/pages/list.jsp
<html>
<body>
<h1>Spring MVC Restful Test</h1>
<h3>message : ${message}</h3>
</body>
</html>
// WEB-INF/pages/command.jsp
<html>
<body>
<h1>Spring MVC Restful Test</h1>
<h3>command id : ${id}</h3>
<h3>content : ${content}</h3>
</body>
</html>
// WEB-INF/pages/delete.jsp
<html>
<body>
<h1>Spring MVC Restful Test</h1>
<h3>deleted id : ${id}</h3>
</body>
</html>
테스트결과
자세한 것은 PPT 또는 프로젝트로 확인
웹콜 테스트
시그윈 또는 linux 테스트
curl -X DELETE http://localhost:8080/restful/link/1
- HTTP 1.1 스펙에서 정의
- 코드
FruitController.java
@Controller
@RequestMapping("/fruit")
public class FruitController {
@RequestMapping(value="{fruitName}", method = RequestMethod.GET)
public String getFruit(@PathVariable String fruitName, ModelMap model) {
Fruit fruit = new Fruit(fruitName, 1000);
model.addAttribute("model", fruit);
return "listfruit";
}
}
mvc-dispatcher-servlet.xml
결과
$ curl -H 'Accept: application/xml' localhost:8080/fruit/banana.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><fruit><name>banana</name
><quality>1000</quality></fruit>
$ curl -H 'Accept: application/rss' localhost:8080/fruit/banana.rss
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>Sample Title</title>
<link>http://google.com</link>
<description>Sample Description</description>
<item>
<link>http://www.google.com</link>
<content:encoded>banana1000</content:encoded>
<author>Test</author>
</item>
</channel>
</rss>
$ curl -H 'Accept: application/json' localhost:8080/fruit/banana.json
{"model":{"quality":1000,"name":"banana"}}
웹브라우져에서는 PUT/DELETE를 쓸 수 없다.
- HTML5에서 put/delete는 사용하지 못한다고 나와 있음
Spring MVC는 이 것을 가능하게 함
=> 자세한 것은 PPT 참조
클라이언트 API - RestTemplate
기존의 JDBC Template과 비슷.
Map<String, String> vars = new HashMap<String, String>();
vars.put("id", "111");
vars.put("content", "222");
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/restful/command/{id}/content/{content}", String.class, vars);
System.out.println("result : " + result);
결과
result : <html>
<body>
<h1>Spring MVC Restful Test</h1>
<h3>command id : 111</h3>
<h3>content : 222</h3>
</body>
</html>
'general java' 카테고리의 다른 글
WindowBuilder Pro 간단한 소개 (0) | 2011.09.20 |
---|---|
OSCache 3가지 주의할 점 (0) | 2011.09.07 |
java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV (0) | 2011.08.08 |
Jersey Framework 맛보기 (0) | 2011.08.05 |
구글 프로토콜 버퍼 (Google Protocl Buffer) (0) | 2011.08.03 |