Skip to content

Instantly share code, notes, and snippets.

@mallim
Created October 21, 2013 02:23
Show Gist options
  • Save mallim/7077819 to your computer and use it in GitHub Desktop.
Save mallim/7077819 to your computer and use it in GitHub Desktop.
Jersey @put and @delete with Backbone.emulateHTTP set to true
import com.sun.jersey.api.view.Viewable;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;
@Path("/try")
@Service
public class TryResource {
private static Logger log = Logger.getLogger( TryResource.class );
@GET
@Path("/view")
public Response getView(){
Viewable viewable = new Viewable( "/try" );
return Response.ok( viewable ).build();
}
@PUT
@Path("/crud/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Map<String,Object> saveTry( @PathParam("id") String id, Map<String,Object> params )
{
log.debug( "SAVING, id=" + id );
log.debug( "input parameters = " + params );
return params;
}
@DELETE
@Path( "/crud/{id}")
public Response deleteTry( @PathParam("id") String id )
{
log.debug( "DELETE, id=" + id );
return Response.ok().build();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Try Jersey's @PUT and @DELETE</title>
<meta name="description" content="Try">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/normalize.min.css">
<script src="<%=request.getContextPath()%>/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="overflow-x:auto">
<script src="<%=request.getContextPath()%>/js/vendor/jquery-1.10.2.min.js"></script>
<script src="<%=request.getContextPath()%>/js/vendor/underscore-1.5.1.min.js"></script>
<script src="<%=request.getContextPath()%>/js/vendor/backbone-1.0.0.min.js"></script>
<script>
Backbone.emulateHTTP = true;
var UserModel = Backbone.Model.extend({
urlRoot: '/try/crud',
defaults: {
name: '',
email: ''
}
});
// Here we have set the `id` of the model
var user = new UserModel({
id: 1,
name: 'Thomas',
email: 'thomasalwyndavis@gmail.com'
});
// Let's change the name and update the server
// Because there is `id` present, Backbone.js will fire
// PUT /user/1 with a payload of `{name: 'Davis', email: 'thomasalwyndavis@gmail.com'}`
user.save({name: 'Davis'}, {
success: function (model) {
console.log("SAVE...",user.toJSON());
}
});
// Because there is `id` present, Backbone.js will fire
// DELETE /user/1
user.destroy({
success: function ( model ) {
console.log("DESTROY...",user.toJSON());
}
});
</script>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true"
>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>jersey-filter</filter-name>
<filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.ImplicitViewables</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>
/(js|images|css|(WEB-INF/views))/.*
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/views</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>
</init-param>
<!--
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Trace</param-name>
<param-value>true</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>jersey-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment