package com.mycompany.multiplereply;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
@XmlRootElement
public class MyBean implements Serializable {
private String name;
private String sex;
private int age;
public MyBean() {
super();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("<h1>%s</h1>", name));
sb.append("<ul>");
sb.append(String.format("<li>SEX: %s</li>", sex));
sb.append(String.format("<li>AGE: %d</li>", age));
sb.append("</ul>");
return sb.toString();
}
}
package com.mycompany.multiplereply;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
@Path("generic")
public class HelloResource {
@Context
private UriInfo context;
public HelloResource() {
}
@GET
@Path("hello")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_HTML})
public MyBean getSomeFormat() {
return new MyBean("Sachi","female",32);
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
<button id="btnJson">JSON</button>
<button id="btnXml">XML</button>
<button id="btnHtml">HTML</button><br/>
<textarea cols="80" rows="5"></textarea>
<div></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btnJson').on('click', function(){
var args = {
};
var url = 'webresources/generic/hello';
$.getJSON(url, args, function(data){
$('textarea').text(JSON.stringify(data));
});
});
$('#btnXml').on('click', function(){
var url = 'webresources/generic/hello';
$.ajax({
url : url,
dataType : 'xml'
}).done(function(data){
console.log(data);
$('textarea').text((new XMLSerializer()).serializeToString(data));
});
});
$('#btnHtml').on('click', function(){
var url = 'webresources/generic/hello';
$.ajax({
url : url,
dataType : 'html'
}).done(function(data){
console.log(data);
$('textarea').text(data);
$('div').append(data);
});
});
});
</script>
</body>
</html>
package com.mycompany.multiplereply;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
@Provider
@Produces(MediaType.TEXT_HTML)
public class MyBeanDomWrite implements MessageBodyWriter<MyBean> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == MyBean.class;
}
@Override
public long getSize(MyBean t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// -1 means unknown
return -1;
}
@Override
public void writeTo(MyBean t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
PrintWriter out = new PrintWriter(entityStream);
out.print(t);
out.close();
}
}
package com.mycompany.multiplereply;
import java.util.Set;
import javax.ws.rs.core.Application;
/**
*
* @author hondou
*/
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
// following code can be used to customize Jersey 1.x JSON provider:
try {
Class jacksonProvider = Class.forName("org.codehaus.jackson.jaxrs.JacksonJsonProvider");
resources.add(jacksonProvider);
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.mycompany.multiplereply.HelloResource.class);
resources.add(com.mycompany.multiplereply.MyBeanDomWrite.class);
}
}