Spring JSONP with Jackson
This commit is contained in:
parent
a661e0e3c9
commit
9b30521a3f
|
@ -1,56 +1,80 @@
|
|||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.Company;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.MatrixVariable;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.model.Company;
|
||||
|
||||
@Controller
|
||||
public class CompanyController {
|
||||
|
||||
Map<Long, Company> companyMap = new HashMap<>();
|
||||
Map<Long, Company> companyMap = new HashMap<>();
|
||||
|
||||
@RequestMapping(value = "/company", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("companyHome", "company", new Company());
|
||||
}
|
||||
@RequestMapping(value = "/company", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("companyHome", "company", new Company());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/company/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
|
||||
public @ResponseBody Company getCompanyById(@PathVariable final long Id) {
|
||||
return companyMap.get(Id);
|
||||
}
|
||||
@RequestMapping(value = "/company/{Id}", produces = { "application/json",
|
||||
"application/xml" }, method = RequestMethod.GET)
|
||||
public @ResponseBody Company getCompanyById(@PathVariable final long Id) {
|
||||
return companyMap.get(Id);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
|
||||
public String submit(@ModelAttribute("company") final Company company, final BindingResult result, final ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
model.addAttribute("name", company.getName());
|
||||
model.addAttribute("id", company.getId());
|
||||
@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
|
||||
public String submit(@ModelAttribute("company") final Company company,
|
||||
final BindingResult result, final ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
model.addAttribute("name", company.getName());
|
||||
model.addAttribute("id", company.getId());
|
||||
|
||||
companyMap.put(company.getId(), company);
|
||||
companyMap.put(company.getId(), company);
|
||||
|
||||
return "companyView";
|
||||
}
|
||||
return "companyView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
|
||||
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
|
||||
}
|
||||
@RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> getEmployeeDataFromCompany(
|
||||
@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
|
||||
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) {
|
||||
final Map<String, String> result = new HashMap<String, String>();
|
||||
result.put("name", name);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> getCompanyName(
|
||||
@MatrixVariable(value = "name", pathVar = "company") final String name) {
|
||||
final Map<String, String> result = new HashMap<String, String>();
|
||||
result.put("name", name);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/companyResponseBody", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@ResponseBody
|
||||
public Company getCompanyResponseBody() {
|
||||
final Company company = new Company(2, "ABC");
|
||||
return company;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/companyResponseEntity", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<Company> getCompanyResponseEntity() {
|
||||
final Company company = new Company(3, "123");
|
||||
return new ResponseEntity<Company>(company, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.web.controller.advice;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
|
||||
|
||||
@ControllerAdvice
|
||||
public class JsonpControllerAdvice extends AbstractJsonpResponseBodyAdvice {
|
||||
|
||||
public JsonpControllerAdvice() {
|
||||
super("callback");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="ISO-8859-1">
|
||||
<title>Company Data</title>
|
||||
<script>
|
||||
function getCompanyData(data) {
|
||||
document.write("<b>ID:</b> "+data.id+"<br/>");
|
||||
document.write("<b>NAME:</b> "+data.name+"<br/>");
|
||||
document.write("<br/>");
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<!--Using @ResponseBody -->
|
||||
<script
|
||||
src="http://localhost:8080/spring-mvc-java/companyResponseBody?callback=getCompanyData"
|
||||
type="application/javascript"> </script>
|
||||
|
||||
<!--Using ResponseEntity -->
|
||||
<script
|
||||
src="http://localhost:8080/spring-mvc-java/companyResponseEntity?callback=getCompanyData"
|
||||
type="application/javascript"> </script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import org.baeldung.web.dto.Company;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class CompanyController {
|
||||
|
||||
@RequestMapping(value = "/companyRest", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Company getCompanyRest() {
|
||||
final Company company = new Company(1, "Xpto");
|
||||
return company;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.baeldung.web.controller.advice;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
|
||||
|
||||
@ControllerAdvice
|
||||
public class JsonpControllerAdvice extends AbstractJsonpResponseBodyAdvice {
|
||||
|
||||
public JsonpControllerAdvice() {
|
||||
super("callback");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package org.baeldung.web.dto;
|
||||
|
||||
public class Company {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public Company() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Company(final long id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Company [id=" + id + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="ISO-8859-1">
|
||||
<title>Company Data</title>
|
||||
<script>
|
||||
function getCompanyData(data) {
|
||||
document.write("<b>ID:</b> "+data.id+"<br/>");
|
||||
document.write("<b>NAME:</b> "+data.name+"<br/>");
|
||||
document.write("<br/>");
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<!-- Using REST URL-->
|
||||
<script
|
||||
src="http://localhost:8080/spring-rest/companyRest?callback=getCompanyData"
|
||||
type="application/javascript"> </script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue