don't dream your life, live your dreams !
Contents
Please read : Java spring – quickstart
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); } /* * Configure ContentNegotiatingViewResolver */ @Bean public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager(manager); // Define all possible view resolvers List<ViewResolver> resolvers = new ArrayList<>(); resolver.setViewResolvers(resolvers); return resolver; } |
Let’s deal with the following model :
@XmlRootElement(name = "contact") @Entity @Table(name="CONTACTS") public class Contact { @Id @Column(name="ID") @GeneratedValue private Integer id; @Column(name="FIRSTNAME") private String firstname; @Column(name="LASTNAME") private String lastname; @Column(name="EMAIL") private String email; @Column(name="TELEPHONE") private String telephone; public String getEmail() { return email; } public String getTelephone() { return telephone; } public void setEmail(String email) { this.email = email; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getFirstname() { return firstname; } public String getLastname() { return lastname; } public void setFirstname(String firstname) { this.firstname = firstname; } public void setLastname(String lastname) { this.lastname = lastname; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } } |
The rest controller can have the following implementation:
@RestController public class RestApiController { @RequestMapping(value = "/rest", method = RequestMethod.GET) public Contact rest(Map<String, Object> map) { Contact contact = new Contact(); contact.setId(0); contact.setEmail("john.doe@mail.com"); contact.setFirstname("john"); contact.setLastname("doe"); contact.setTelephone("123456"); return Contact; } } |
The tiles controller can have the following implementation:
@Controller public class TilesController { @RequestMapping(value = "/contact", method = RequestMethod.GET) public String contact(Map<String, Object> map) { Contact contact = new Contact(); contact.setId(0); contact.setEmail("john.doe@mail.com"); contact.setFirstname("john"); contact.setLastname("doe"); contact.setTelephone("123456"); model.addAttribute("contact", contact); return "/model/contact/read"; } } |
If you request url : http://localhost:8080/contact.html, you will be redirected to the jsp view (i.e. model/contact/read.jsp file) :
<!DOCTYPE html> <html> <head> </head> <body> <h2>Header</h2> <ul> <li id="contact-id">0</li> <li id="contact-firstname">john</li> <li id="contact-lastname">doe</li> <li id="contact-email">john.doe@mail.com</li> <li id="contact-telephone">123456</li> </ul> <h2>Footer</h2> </body> </html> |
If you request url : http://localhost:8080/rest.json, you will be redirected to the json view :
{"contact":{"id":0,"firstname":"john","lastname":"doe","email":"john.doe@mail.com","telephone":"123456"}} |
If you request url : http://localhost:8080/rest.xml , you will be redirected to the xml view :
<contact> <email>john.doe@mail.com</email> <firstname>john</firstname> <id>0</id> <lastname>doe</lastname> <telephone>123456</telephone> </contact> |
Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.
admin