Jersey(JAX-RS)でRESTful API開発–@Producesと @Consumesの使い方

1.Javaコード
@Path(“CompanyInfo”)
public class CompanyInfo {
@GET
@Produces(“application/xml”)
@Consumes(“text/plain”)
public String getXML(@QueryParam(“name”) @DefaultValue(“○○株式会社”) String companyName) throws Exception{
DcoumentBuilderfactory dbf =DocumentBuilderfactory.newInstance();
DocumentBuilder builder =dbf.newDcoumentBuilder();
Document doc = builder.newDocument();

Element element = doc.createElement(“会社”);
element.appendChild(doc.createElement(“開発チーム”));
element.appendChild(doc.createElement(“インフラチーム”));
element.setAttribute(“社名”, companyName);
doc.appendChild(element);

StringWriter strSwr = new StringWriter();
StreamResult strSrt = new StreamResult(strSwr);
TransformerFactory strTfac = TransformerFactory.newInstance();
Transformer tr = strTfac.newTransformer();
tr.transform(new DOMSource(doc.getDocumentElement()),strSrt);
String strRet = strSrt.getWriter().toString();
return strRet;
}
}

2.サーブレット用web.xml
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

3.検証URL
http://localhost:8080/rest/services/CompanyInfo
http://localhost:8080/rest/services/CompanyInfo?name=itcompany