{"id":1594,"date":"2016-10-04T16:11:41","date_gmt":"2016-10-04T14:11:41","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1594"},"modified":"2017-05-11T12:40:31","modified_gmt":"2017-05-11T10:40:31","slug":"java-spring-tilesrest","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1594","title":{"rendered":"Java Spring &#8211; tiles+rest"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Prerequistes\"><span class=\"toc_number toc_depth_1\">1<\/span> Prerequistes<\/a><\/li><li><a href=\"#Configuration\"><span class=\"toc_number toc_depth_1\">2<\/span> Configuration<\/a><ul><li><a href=\"#MediaConfigjava\"><span class=\"toc_number toc_depth_2\">2.1<\/span> MediaConfig.java<\/a><\/li><li><a href=\"#Model\"><span class=\"toc_number toc_depth_2\">2.2<\/span> Model<\/a><\/li><li><a href=\"#RestController\"><span class=\"toc_number toc_depth_2\">2.3<\/span> RestController<\/a><\/li><li><a href=\"#TilesController\"><span class=\"toc_number toc_depth_2\">2.4<\/span> TilesController<\/a><\/li><\/ul><\/li><li><a href=\"#How_to_use_it\"><span class=\"toc_number toc_depth_1\">3<\/span> How to use it<\/a><ul><li><a href=\"#html_view\"><span class=\"toc_number toc_depth_2\">3.1<\/span> html view<\/a><\/li><li><a href=\"#Header\"><span class=\"toc_number toc_depth_2\">3.2<\/span> Header<\/a><\/li><li><a href=\"#Footer\"><span class=\"toc_number toc_depth_2\">3.3<\/span> Footer<\/a><\/li><li><a href=\"#json_view\"><span class=\"toc_number toc_depth_2\">3.4<\/span> json view<\/a><\/li><li><a href=\"#xml_view\"><span class=\"toc_number toc_depth_2\">3.5<\/span> xml view<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Prerequistes\">Prerequistes<\/span><\/h1>\n<p>Please read : <a href=\"?page_id=1549\">Java spring &#8211; quickstart<\/a><\/p>\n<h1><span id=\"Configuration\">Configuration<\/span><\/h1>\n<h2><span id=\"MediaConfigjava\">MediaConfig.java<\/span><\/h2>\n<p>You have to define the following beans :<\/p>\n<pre lang=\"java\">\r\n@Configuration\r\npublic class MediaConfig extends WebMvcConfigurerAdapter {\r\n\r\n\t@Override\r\n\tpublic void configureContentNegotiation(ContentNegotiationConfigurer configurer) {\r\n\t\tconfigurer\r\n\t\t\/\/.ignoreAcceptHeader(true)\r\n\t\t.defaultContentType(MediaType.TEXT_HTML)\r\n\t\t.favorPathExtension(true);\r\n\t}\r\n\t\r\n\t\/*\r\n\t * Configure ContentNegotiatingViewResolver\r\n\t *\/\r\n\t@Bean\r\n\tpublic ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {\r\n\t\tContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();\r\n\t\tresolver.setContentNegotiationManager(manager);\r\n\r\n\t\t\/\/ Define all possible view resolvers\r\n\t\tList<ViewResolver> resolvers = new ArrayList<>();\r\n\t\tresolver.setViewResolvers(resolvers);\r\n\t\treturn resolver;\r\n\t}\r\n<\/pre>\n<h2><span id=\"Model\">Model<\/span><\/h2>\n<p>Let&#8217;s deal with the following model :<\/p>\n<pre lang=\"java\">\r\n@XmlRootElement(name = \"contact\")\r\n@Entity\r\n@Table(name=\"CONTACTS\")\r\npublic class Contact {\r\n\r\n\t@Id\r\n\t@Column(name=\"ID\")\r\n\t@GeneratedValue\r\n\tprivate Integer id;\r\n\r\n\t@Column(name=\"FIRSTNAME\")\r\n\tprivate String firstname;\r\n\r\n\t@Column(name=\"LASTNAME\")\r\n\tprivate String lastname;\r\n\r\n\t@Column(name=\"EMAIL\")\r\n\tprivate String email;\r\n\r\n\t@Column(name=\"TELEPHONE\")\r\n\tprivate String telephone;\r\n\r\n\r\n\tpublic String getEmail() {\r\n\t\treturn email;\r\n\t}\r\n\tpublic String getTelephone() {\r\n\t\treturn telephone;\r\n\t}\r\n\tpublic void setEmail(String email) {\r\n\t\tthis.email = email;\r\n\t}\r\n\tpublic void setTelephone(String telephone) {\r\n\t\tthis.telephone = telephone;\r\n\t}\r\n\tpublic String getFirstname() {\r\n\t\treturn firstname;\r\n\t}\r\n\tpublic String getLastname() {\r\n\t\treturn lastname;\r\n\t}\r\n\tpublic void setFirstname(String firstname) {\r\n\t\tthis.firstname = firstname;\r\n\t}\r\n\tpublic void setLastname(String lastname) {\r\n\t\tthis.lastname = lastname;\r\n\t}\r\n\tpublic Integer getId() {\r\n\t\treturn id;\r\n\t}\r\n\tpublic void setId(Integer id) {\r\n\t\tthis.id = id;\r\n\t}\r\n}\r\n<\/pre>\n<h2><span id=\"RestController\">RestController<\/span><\/h2>\n<p>The rest controller can have the following implementation:<\/p>\n<pre lang=\"java\">\r\n@RestController\r\npublic class RestApiController {\r\n\r\n  @RequestMapping(value = \"\/rest\", method = RequestMethod.GET)\r\n  public Contact rest(Map<String, Object> map) {\r\n\t\tContact contact = new Contact();\r\n\t\tcontact.setId(0);\r\n\t\tcontact.setEmail(\"john.doe@mail.com\");\r\n\t\tcontact.setFirstname(\"john\");\r\n\t\tcontact.setLastname(\"doe\");\r\n\t\tcontact.setTelephone(\"123456\");\r\n\t\treturn Contact;\r\n  }\r\n}\r\n<\/pre>\n<h2><span id=\"TilesController\">TilesController<\/span><\/h2>\n<p>The tiles controller can have the following implementation:<\/p>\n<pre lang=\"java\">\r\n@Controller\r\npublic class TilesController {\r\n\r\n  @RequestMapping(value = \"\/contact\", method = RequestMethod.GET)\r\n  public String contact(Map<String, Object> map) {\r\n\t\tContact contact = new Contact();\r\n\t\tcontact.setId(0);\r\n\t\tcontact.setEmail(\"john.doe@mail.com\");\r\n\t\tcontact.setFirstname(\"john\");\r\n\t\tcontact.setLastname(\"doe\");\r\n\t\tcontact.setTelephone(\"123456\");\r\n\r\n\t\tmodel.addAttribute(\"contact\", contact);\r\n                return \"\/model\/contact\/read\";\r\n  }\r\n}\r\n<\/pre>\n<h1><span id=\"How_to_use_it\">How to use it<\/span><\/h1>\n<h2><span id=\"html_view\">html view<\/span><\/h2>\n<p>If you request url : http:\/\/localhost:8080\/contact.html, you will be redirected to the jsp view (i.e. model\/contact\/read.jsp file) : <\/p>\n<pre lang=\"html\">\r\n<!DOCTYPE html>\r\n\r\n<html>\r\n  <head>\r\n  <\/head>\r\n\r\n<body>\r\n\r\n<h2><span id=\"Header\">Header<\/span><\/h2>\r\n\r\n <ul>\r\n   <li id=\"contact-id\">0<\/li>\r\n   <li id=\"contact-firstname\">john<\/li>\r\n   <li id=\"contact-lastname\">doe<\/li>\r\n   <li id=\"contact-email\">john.doe@mail.com<\/li>\r\n   <li id=\"contact-telephone\">123456<\/li>\r\n   \r\n <\/ul>\r\n\r\n<h2><span id=\"Footer\">Footer<\/span><\/h2>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<h2><span id=\"json_view\">json view<\/span><\/h2>\n<p>If you request url : http:\/\/localhost:8080\/rest.json, you will be redirected to the json view :<\/p>\n<pre lang=\"json\">\r\n{\"contact\":{\"id\":0,\"firstname\":\"john\",\"lastname\":\"doe\",\"email\":\"john.doe@mail.com\",\"telephone\":\"123456\"}}\r\n<\/pre>\n<h2><span id=\"xml_view\">xml view<\/span><\/h2>\n<p>If you request url : http:\/\/localhost:8080\/rest.xml , you will be redirected to the xml view :<\/p>\n<pre lang=\"xml\">\r\n<contact>\r\n  <email>john.doe@mail.com<\/email>\r\n  <firstname>john<\/firstname>\r\n  <id>0<\/id>\r\n  <lastname>doe<\/lastname>\r\n  <telephone>123456<\/telephone>\r\n<\/contact>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Prerequistes2 Configuration2.1 MediaConfig.java2.2 Model2.3 RestController2.4 TilesController3 How to use it3.1 html view3.2 Header3.3 Footer3.4 json view3.5 xml view Prerequistes Please read : Java spring &#8211; quickstart Configuration MediaConfig.java You have to define the following beans : @Configuration public class MediaConfig extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer \/\/.ignoreAcceptHeader(true) .defaultContentType(MediaType.TEXT_HTML) .favorPathExtension(true); } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1547,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1594","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1594"}],"version-history":[{"count":6,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1594\/revisions"}],"predecessor-version":[{"id":1984,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1594\/revisions\/1984"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1547"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}