DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kevin has posted 4 posts at DZone. View Full User Profile

Resteasy @MatrixParam And @PathSegment Examples

07.21.2009
| 7188 views |
  • submit to reddit
        
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.PathSegment;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.jboss.resteasy.annotations.providers.jaxb.Wrapped;

/**
 * 
 */
@Path("/")
public class MatrixParamRestService {
    
	@GET
	@Path("/matrixparams")
	@Produces("application/xml")
	@Wrapped
	public List<NameValueBean> getMatricParams(@MatrixParam("year") String year, @MatrixParam("month") String month){
		NameValueBean nvby = new NameValueBean();
		nvby.setName("year");
		nvby.setValue(year);
		
		NameValueBean nvbm = new NameValueBean();
		nvbm.setName("month");
		nvbm.setValue(month);
		
		List<NameValueBean> list = new ArrayList<NameValueBean>();
		list.add(nvby);
		list.add(nvbm);
		
		return list;
	}
	
	@GET
	@Path("/pathsegment/{segment}")
	@Produces("application/xml")
	@Wrapped
	public List<NameValueBean> getPathSegment(@PathParam("segment") PathSegment segment){
		
		String path = segment.getPath();
		
		NameValueBean nvbp = new NameValueBean();
		nvbp.setName("path");
		nvbp.setValue(path);
		
		MultivaluedMap<String, String> mvm = segment.getMatrixParameters();
				
		NameValueBean nvby = new NameValueBean();
		nvby.setName("year");
		nvby.setValue(mvm.getFirst("year"));
		
		NameValueBean nvbm = new NameValueBean();
		nvbm.setName("month");
		nvbm.setValue(mvm.getFirst("month"));
		
		List<NameValueBean> list = new ArrayList<NameValueBean>();
		
		list.add(nvbp);
		list.add(nvby);
		list.add(nvbm);
		
		return list;
	}	
		
	
	
	@XmlRootElement(name="data")
	@XmlAccessorType(XmlAccessType.FIELD)
	private static class NameValueBean{
		@XmlElement
		private String name;
		
		@XmlElement
		private String value;
		
		public NameValueBean(){
		}
		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}
		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}
		/**
		 * @return the value
		 */
		public String getValue() {
			return value;
		}
		/**
		 * @param value the value to set
		 */
		public void setValue(String value) {
			this.value = value;
		}
	}
}