restful 의 resource 클래스 의 각종 수신 매개 변수 방식

3185 단어 RESTFUajax
처음에 @ Query Param 으로 인 자 를 받 았 는데 나중에 공부 하 다가 @ PathParam 도 받 을 수 있 는 것 을 발 견 했 습 니 다. 그러나 용법 이 다 릅 니 다. 나룻배 를 건 너 서 자신 이 여기 서 정 리 를 했 습 니 다.
 、@PathParam (@PathParam ,url             

가령http://localhost:8181/managerinfo/rs/helloworld/user/frank
resource 의 작성 방법 은:
@GET
	@Path("/user/{condition}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_HTML)
	public String getHelloWithPath(@PathParam("condition") String username) {
		return "Hello Path UserName " + username;
	}
 @QueryParam  (url      【   】     

ajax 로 요청 
$.ajax({
             type: 'GET',
             contentType: "application/json",
             dataType: 'html',
             url: 'http://localhost:8181/managerinfo/rs/helloworld/user',
             data: {
                  username:"frank"
	     },
	     success:function(data){
	          alert(data);
	     }

실제 요청 주 소 는:http://localhost:8181/managerinfo/rs/helloworld/user?username=frank
다음 과 같은 몇 가지 표기 법 은 다음 과 같다.
http://blog.csdn.net/azhegps/article/details/72782704
여 기 는 자신 이 사용 하지 않 았 습 니 다. 기록 하 세 요.
3. @ DefaultValue
예: @GET @Path("/user") @Produces("text/plain") public User getUser(@QueryParam("name") String name,                     @DefaultValue("26") @QueryParam("age") int age) {     ... } 브 라 우 저 요청http://host:port/user?name=rose시, name 값 은 rose 이 고, age 값 은 26 입 니 다. 4. @ FormParam @FormParam, 말 그대로 POST 가 요청 한 폼 매개 변수 에서 데 이 터 를 가 져 옵 니 다. 예 를 들 어: @POST @Consumes("application/x-www-form-urlencoded") publicvoid post(@FormParam("name") String name) {     // Store the message } html 를 사용 하여 post 를 제출 한 사람 이 폼 에 대해 낯 설 지 않 을 것 이 라 고 믿 습 니 다. 이것 이 바로 html 에서 폼 을 모 의 하 는 요청 이 라 고 상상 할 수 있 습 니 다. 5. 사용 지도 대형 server 에서 매개 변수의 변화 로 인해 매개 변수 구조의 조정 은 상기 몇 가지 방식 으로 인해 문제 가 발생 할 수 있 습 니 다. 이 때 @ Context 주석 을 사용 하고 UriInfo 인 스 턴 스 를 얻 는 것 을 고려 할 수 있 습 니 다. 다음 과 같 습 니 다. @GET public String get(@Context UriInfo ui) {     MultivaluedMap queryParams = ui.getQueryParameters();     MultivaluedMap pathParams = ui.getPathParameters(); } 맵 은 위의 몇 가지 상황 의 초 집합 이 라 고 생각 할 수 있 습 니 다. 왜냐하면 그것 은 위의 임의의 것 을 대체 할 수 있 기 때 문 입 니 다. 맵 은 context 입 니 다. 
       @Context     ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse HttpHeaders ,  : 
@Path("/") 
publicclass Resource { 

    @Context 
    HttpServletRequest req; 

    @Context 
    ServletConfig servletConfig; 

    @Context 
    ServletContext servletContext; 

    @GET 
    public String get(@Context HttpHeaders hh) { 
        MultivaluedMap headerParams = hh.getRequestHeaders(); 
        Map pathParams = hh.getCookies(); 
    } 
}  

좋은 웹페이지 즐겨찾기