JAVA-3526: Moved spring-mvc-java inside spring-web-modules
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringMVCApplication extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringMVCApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Sample rest controller for the tutorial article
|
||||
* "Access Spring MVC Model object in JavaScript".
|
||||
*
|
||||
* @author Andrew Shcherbakov
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
/**
|
||||
* Define two model objects (one integer and one string) and pass them to the view.
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/index")
|
||||
public ModelAndView thymeleafView(Map<String, Object> model) {
|
||||
model.put("number", 1234);
|
||||
model.put("message", "Hello from Spring MVC");
|
||||
return new ModelAndView("thymeleaf/index");
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.cache;
|
||||
|
||||
import com.baeldung.model.Book;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class BookService {
|
||||
|
||||
@Cacheable(value="books", keyGenerator="customKeyGenerator")
|
||||
public List<Book> getBooks() {
|
||||
List<Book> books = new ArrayList<Book>();
|
||||
books.add(new Book(1, "The Counterfeiters", "André Gide"));
|
||||
books.add(new Book(2, "Peer Gynt and Hedda Gabler", "Henrik Ibsen"));
|
||||
return books;
|
||||
}
|
||||
|
||||
}
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.cache;
|
||||
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class CustomKeyGenerator implements KeyGenerator {
|
||||
|
||||
public Object generate(Object target, Method method, Object... params) {
|
||||
return target.getClass().getSimpleName() + "_" + method.getName() + "_"
|
||||
+ StringUtils.arrayToDelimitedString(params, "_");
|
||||
}
|
||||
}
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
package com.baeldung.excel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||
import org.apache.poi.xssf.usermodel.XSSFFont;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
||||
import org.apache.poi.hssf.usermodel.HSSFFont;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ExcelPOIHelper {
|
||||
|
||||
public Map<Integer, List<MyCell>> readExcel(String fileLocation) throws IOException {
|
||||
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
FileInputStream fis = new FileInputStream(new File(fileLocation));
|
||||
|
||||
if (fileLocation.endsWith(".xls")) {
|
||||
data = readHSSFWorkbook(fis);
|
||||
} else if (fileLocation.endsWith(".xlsx")) {
|
||||
data = readXSSFWorkbook(fis);
|
||||
}
|
||||
|
||||
int maxNrCols = data.values().stream()
|
||||
.mapToInt(List::size)
|
||||
.max()
|
||||
.orElse(0);
|
||||
|
||||
data.values().stream()
|
||||
.filter(ls -> ls.size() < maxNrCols)
|
||||
.forEach(ls -> {
|
||||
IntStream.range(ls.size(), maxNrCols)
|
||||
.forEach(i -> ls.add(new MyCell("")));
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private String readCellContent(Cell cell) {
|
||||
String content;
|
||||
switch (cell.getCellTypeEnum()) {
|
||||
case STRING:
|
||||
content = cell.getStringCellValue();
|
||||
break;
|
||||
case NUMERIC:
|
||||
if (DateUtil.isCellDateFormatted(cell)) {
|
||||
content = cell.getDateCellValue() + "";
|
||||
} else {
|
||||
content = cell.getNumericCellValue() + "";
|
||||
}
|
||||
break;
|
||||
case BOOLEAN:
|
||||
content = cell.getBooleanCellValue() + "";
|
||||
break;
|
||||
case FORMULA:
|
||||
content = cell.getCellFormula() + "";
|
||||
break;
|
||||
default:
|
||||
content = "";
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
private Map<Integer, List<MyCell>> readHSSFWorkbook(FileInputStream fis) throws IOException {
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
HSSFWorkbook workbook = null;
|
||||
try {
|
||||
workbook = new HSSFWorkbook(fis);
|
||||
|
||||
HSSFSheet sheet = workbook.getSheetAt(0);
|
||||
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
|
||||
HSSFRow row = sheet.getRow(i);
|
||||
data.put(i, new ArrayList<>());
|
||||
if (row != null) {
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) {
|
||||
HSSFCell cell = row.getCell(j);
|
||||
if (cell != null) {
|
||||
HSSFCellStyle cellStyle = cell.getCellStyle();
|
||||
|
||||
MyCell myCell = new MyCell();
|
||||
|
||||
HSSFColor bgColor = cellStyle.getFillForegroundColorColor();
|
||||
if (bgColor != null) {
|
||||
short[] rgbColor = bgColor.getTriplet();
|
||||
myCell.setBgColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
|
||||
}
|
||||
HSSFFont font = cell.getCellStyle()
|
||||
.getFont(workbook);
|
||||
myCell.setTextSize(font.getFontHeightInPoints() + "");
|
||||
if (font.getBold()) {
|
||||
myCell.setTextWeight("bold");
|
||||
}
|
||||
HSSFColor textColor = font.getHSSFColor(workbook);
|
||||
if (textColor != null) {
|
||||
short[] rgbColor = textColor.getTriplet();
|
||||
myCell.setTextColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
|
||||
}
|
||||
myCell.setContent(readCellContent(cell));
|
||||
data.get(i)
|
||||
.add(myCell);
|
||||
} else {
|
||||
data.get(i)
|
||||
.add(new MyCell(""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (workbook != null) {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private Map<Integer, List<MyCell>> readXSSFWorkbook(FileInputStream fis) throws IOException {
|
||||
XSSFWorkbook workbook = null;
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
try {
|
||||
|
||||
workbook = new XSSFWorkbook(fis);
|
||||
XSSFSheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
|
||||
XSSFRow row = sheet.getRow(i);
|
||||
data.put(i, new ArrayList<>());
|
||||
if (row != null) {
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) {
|
||||
XSSFCell cell = row.getCell(j);
|
||||
if (cell != null) {
|
||||
XSSFCellStyle cellStyle = cell.getCellStyle();
|
||||
|
||||
MyCell myCell = new MyCell();
|
||||
XSSFColor bgColor = cellStyle.getFillForegroundColorColor();
|
||||
if (bgColor != null) {
|
||||
byte[] rgbColor = bgColor.getRGB();
|
||||
myCell.setBgColor("rgb(" + (rgbColor[0] < 0 ? (rgbColor[0] + 0xff) : rgbColor[0]) + "," + (rgbColor[1] < 0 ? (rgbColor[1] + 0xff) : rgbColor[1]) + "," + (rgbColor[2] < 0 ? (rgbColor[2] + 0xff) : rgbColor[2]) + ")");
|
||||
}
|
||||
XSSFFont font = cellStyle.getFont();
|
||||
myCell.setTextSize(font.getFontHeightInPoints() + "");
|
||||
if (font.getBold()) {
|
||||
myCell.setTextWeight("bold");
|
||||
}
|
||||
XSSFColor textColor = font.getXSSFColor();
|
||||
if (textColor != null) {
|
||||
byte[] rgbColor = textColor.getRGB();
|
||||
myCell.setTextColor("rgb(" + (rgbColor[0] < 0 ? (rgbColor[0] + 0xff) : rgbColor[0]) + "," + (rgbColor[1] < 0 ? (rgbColor[1] + 0xff) : rgbColor[1]) + "," + (rgbColor[2] < 0 ? (rgbColor[2] + 0xff) : rgbColor[2]) + ")");
|
||||
}
|
||||
myCell.setContent(readCellContent(cell));
|
||||
data.get(i)
|
||||
.add(myCell);
|
||||
} else {
|
||||
data.get(i)
|
||||
.add(new MyCell(""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (workbook != null) {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.excel;
|
||||
|
||||
public class MyCell {
|
||||
private String content;
|
||||
private String textColor;
|
||||
private String bgColor;
|
||||
private String textSize;
|
||||
private String textWeight;
|
||||
|
||||
public MyCell() {
|
||||
}
|
||||
|
||||
public MyCell(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getTextColor() {
|
||||
return textColor;
|
||||
}
|
||||
|
||||
public void setTextColor(String textColor) {
|
||||
this.textColor = textColor;
|
||||
}
|
||||
|
||||
public String getBgColor() {
|
||||
return bgColor;
|
||||
}
|
||||
|
||||
public void setBgColor(String bgColor) {
|
||||
this.bgColor = bgColor;
|
||||
}
|
||||
|
||||
public String getTextSize() {
|
||||
return textSize;
|
||||
}
|
||||
|
||||
public void setTextSize(String textSize) {
|
||||
this.textSize = textSize;
|
||||
}
|
||||
|
||||
public String getTextWeight() {
|
||||
return textWeight;
|
||||
}
|
||||
|
||||
public void setTextWeight(String textWeight) {
|
||||
this.textWeight = textWeight;
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.filters;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebFilter(urlPatterns = "/uppercase")
|
||||
public class EmptyParamFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
|
||||
FilterChain filterChain) throws IOException, ServletException {
|
||||
String inputString = servletRequest.getParameter("input");
|
||||
|
||||
if (inputString != null && inputString.matches("[A-Za-z0-9]+")) {
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} else {
|
||||
servletResponse.getWriter().println("Missing input parameter");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.listeners;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class AppListener implements ServletContextListener {
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent event) {
|
||||
ServletContext context = event.getServletContext();
|
||||
context.setAttribute("counter", 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent event) {
|
||||
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.listeners;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletRequestEvent;
|
||||
import javax.servlet.ServletRequestListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@WebListener
|
||||
public class RequestListener implements ServletRequestListener {
|
||||
|
||||
@Override
|
||||
public void requestInitialized(ServletRequestEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestDestroyed(ServletRequestEvent event) {
|
||||
HttpServletRequest request = (HttpServletRequest)event.getServletRequest();
|
||||
if (!request.getServletPath().equals("/counter")) {
|
||||
ServletContext context = event.getServletContext();
|
||||
context.setAttribute("counter", (int)context.getAttribute("counter") + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class Article {
|
||||
|
||||
public static final Article DEFAULT_ARTICLE = new Article(12);
|
||||
|
||||
private Integer id;
|
||||
|
||||
public Article(Integer articleId) {
|
||||
this.id = articleId;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Article [id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class Book {
|
||||
|
||||
private int id;
|
||||
private String author;
|
||||
private String title;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(int id, String author, String title) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class FormDataWithFile {
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
private MultipartFile file;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public MultipartFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(MultipartFile file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class Greeting {
|
||||
private int id;
|
||||
private String message;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class User {
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private String emailId;
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public void setFirstname(final String firstname) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
|
||||
public void setLastname(final String lastname) {
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public String getEmailId() {
|
||||
return emailId;
|
||||
}
|
||||
|
||||
public void setEmailId(final String emailId) {
|
||||
this.emailId = emailId;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
@WebServlet(urlPatterns = "/counter", name = "counterServlet")
|
||||
public class CounterServlet extends HttpServlet {
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
int count = (int)request.getServletContext().getAttribute("counter");
|
||||
|
||||
out.println("Request counter: " + count);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
@WebServlet(urlPatterns = "/uppercase", name = "uppercaseServlet")
|
||||
public class UppercaseServlet extends HttpServlet {
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String inputString = request.getParameter("input").toUpperCase();
|
||||
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
out.println(inputString);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import com.baeldung.cache.CustomKeyGenerator;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class ApplicationCacheConfig extends CachingConfigurerSupport {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
Cache booksCache = new ConcurrentMapCache("books");
|
||||
cacheManager.setCaches(Arrays.asList(booksCache));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@Bean("customKeyGenerator")
|
||||
public KeyGenerator keyGenerator() {
|
||||
return new CustomKeyGenerator();
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||
|
||||
private String TMP_FOLDER = "/tmp";
|
||||
private int MAX_UPLOAD_SIZE = 5 * 1024 * 1024;
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext sc) throws ServletException {
|
||||
|
||||
ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(
|
||||
new GenericWebApplicationContext()));
|
||||
|
||||
appServlet.setLoadOnStartup(1);
|
||||
|
||||
MultipartConfigElement multipartConfigElement = new MultipartConfigElement(TMP_FOLDER,
|
||||
MAX_UPLOAD_SIZE, MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2);
|
||||
|
||||
appServlet.setMultipartConfig(multipartConfigElement);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Description;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
import com.baeldung.excel.ExcelPOIHelper;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.web.controller" })
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
registry.addViewController("/").setViewName("index");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver thymeleafViewResolver() {
|
||||
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine());
|
||||
viewResolver.setOrder(1);
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setViewClass(JstlView.class);
|
||||
bean.setPrefix("/WEB-INF/view/");
|
||||
bean.setSuffix(".jsp");
|
||||
bean.setOrder(0);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf template resolver serving HTML 5")
|
||||
public ITemplateResolver templateResolver() {
|
||||
final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/templates/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML");
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf template engine with Spring integration")
|
||||
public SpringTemplateEngine templateEngine() {
|
||||
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver());
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Spring message resolver")
|
||||
public MessageSource messageSource() {
|
||||
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(byteArrayHttpMessageConverter());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
|
||||
final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
|
||||
arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
|
||||
|
||||
return arrayHttpMessageConverter;
|
||||
}
|
||||
|
||||
private List<MediaType> getSupportedMediaTypes() {
|
||||
final List<MediaType> list = new ArrayList<MediaType>();
|
||||
list.add(MediaType.IMAGE_JPEG);
|
||||
list.add(MediaType.IMAGE_PNG);
|
||||
list.add(MediaType.APPLICATION_OCTET_STREAM);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configurePathMatch(final PathMatchConfigurer configurer) {
|
||||
final UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
urlPathHelper.setRemoveSemicolonContent(false);
|
||||
|
||||
configurer.setUrlPathHelper(urlPathHelper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExcelPOIHelper excelPOIHelper() {
|
||||
return new ExcelPOIHelper();
|
||||
}
|
||||
|
||||
@Bean(name = "multipartResolver")
|
||||
public CommonsMultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(100000);
|
||||
return multipartResolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BeanA {
|
||||
|
||||
@Autowired
|
||||
private BeanB b;
|
||||
|
||||
public BeanA() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BeanB {
|
||||
|
||||
public BeanB() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import com.baeldung.excel.*;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Controller
|
||||
public class ExcelController {
|
||||
|
||||
private String fileLocation;
|
||||
|
||||
@Resource(name = "excelPOIHelper")
|
||||
private ExcelPOIHelper excelPOIHelper;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/excelProcessing")
|
||||
public String getExcelProcessingPage() {
|
||||
return "excel";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/uploadExcelFile")
|
||||
public String uploadFile(Model model, MultipartFile file) throws IOException {
|
||||
InputStream in = file.getInputStream();
|
||||
File currDir = new File(".");
|
||||
String path = currDir.getAbsolutePath();
|
||||
fileLocation = path.substring(0, path.length() - 1) + file.getOriginalFilename();
|
||||
FileOutputStream f = new FileOutputStream(fileLocation);
|
||||
int ch = 0;
|
||||
while ((ch = in.read()) != -1) {
|
||||
f.write(ch);
|
||||
}
|
||||
f.flush();
|
||||
f.close();
|
||||
model.addAttribute("message", "File: " + file.getOriginalFilename() + " has been uploaded successfully!");
|
||||
return "excel";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/readPOI")
|
||||
public String readPOI(Model model) throws IOException {
|
||||
|
||||
if (fileLocation != null) {
|
||||
if (fileLocation.endsWith(".xlsx") || fileLocation.endsWith(".xls")) {
|
||||
Map<Integer, List<MyCell>> data = excelPOIHelper.readExcel(fileLocation);
|
||||
model.addAttribute("data", data);
|
||||
} else {
|
||||
model.addAttribute("message", "Not a valid excel file!");
|
||||
}
|
||||
} else {
|
||||
model.addAttribute("message", "File missing! Please upload an excel file.");
|
||||
}
|
||||
return "excel";
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.model.FormDataWithFile;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController {
|
||||
|
||||
@RequestMapping(value = "/fileUpload", method = RequestMethod.GET)
|
||||
public String displayForm() {
|
||||
|
||||
return "fileUploadForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||
public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("file", file);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
|
||||
public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("files", files);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFileWithAddtionalData", method = RequestMethod.POST)
|
||||
public String submit(@RequestParam final MultipartFile file, @RequestParam final String name, @RequestParam final String email, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("name", name);
|
||||
modelMap.addAttribute("email", email);
|
||||
modelMap.addAttribute("file", file);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFileModelAttribute", method = RequestMethod.POST)
|
||||
public String submit(@ModelAttribute final FormDataWithFile formDataWithFile, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("formDataWithFile", formDataWithFile);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.Greeting;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
public class GreetController {
|
||||
|
||||
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
|
||||
public String index() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
|
||||
@ResponseBody
|
||||
public Greeting greet() {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setId(1);
|
||||
greeting.setMessage("Hello World!!!");
|
||||
return greeting;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
|
||||
@ResponseBody
|
||||
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setId(1);
|
||||
greeting.setMessage("Hello World " + name + "!!!");
|
||||
return greeting;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
|
||||
@ResponseBody
|
||||
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setId(1);
|
||||
greeting.setMessage("Hello World " + name + "!!!");
|
||||
return greeting;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
|
||||
@ResponseBody
|
||||
public Greeting greetWithPost() {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setId(1);
|
||||
greeting.setMessage("Hello World!!!");
|
||||
return greeting;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
|
||||
@ResponseBody
|
||||
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setId(id);
|
||||
greeting.setMessage("Hello World " + name + "!!!");
|
||||
return greeting;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
ServletContext servletContext;
|
||||
|
||||
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public @ResponseBody byte[] getImageAsByteArray() throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Controller
|
||||
public class MultipartFileUploadStubController {
|
||||
|
||||
@PostMapping("/stub/multipart")
|
||||
public ResponseEntity<UploadResultResource> uploadFile(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) {
|
||||
UploadResultResource result = new UploadResultResource(file, text, text1, text2, upstream);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
|
||||
public static class UploadResultResource {
|
||||
|
||||
private final String file;
|
||||
private final String text;
|
||||
private final String text1;
|
||||
private final String text2;
|
||||
private final String upstream;
|
||||
|
||||
public UploadResultResource(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) {
|
||||
this.file = format(file);
|
||||
this.text = text;
|
||||
this.text1 = text1;
|
||||
this.text2 = text2;
|
||||
this.upstream = format(upstream);
|
||||
}
|
||||
|
||||
private static String format(MultipartFile file) {
|
||||
return file == null ? null : file.getOriginalFilename() + " (size: " + file.getSize() + " bytes)";
|
||||
}
|
||||
|
||||
public String getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getText1() {
|
||||
return text1;
|
||||
}
|
||||
|
||||
public String getText2() {
|
||||
return text2;
|
||||
}
|
||||
|
||||
public String getUpstream() {
|
||||
return upstream;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class SampleController {
|
||||
@GetMapping("/sample")
|
||||
public String showForm() {
|
||||
return "sample";
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("/")
|
||||
public String showForm(final Model model) {
|
||||
final User user = new User();
|
||||
user.setFirstname("John");
|
||||
user.setLastname("Roy");
|
||||
user.setEmailId("John.Roy@gmail.com");
|
||||
model.addAttribute("user", user);
|
||||
return "index";
|
||||
}
|
||||
|
||||
@PostMapping("/processForm")
|
||||
public String processForm(@ModelAttribute(value = "user") final User user, final Model model) {
|
||||
// Insert User into DB
|
||||
model.addAttribute("name", user.getFirstname() + " " + user.getLastname());
|
||||
return "hello";
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.web.controller.message;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/message")
|
||||
public class MessageController {
|
||||
|
||||
@RequestMapping(value = "/showForm", method = RequestMethod.GET)
|
||||
public String showForm() {
|
||||
return "message";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/processForm", method = RequestMethod.POST)
|
||||
public String processForm(@RequestParam("message") final String message, final Model model) {
|
||||
model.addAttribute("message", message);
|
||||
return "message";
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.web.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
public class ArticleViewerController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.web.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/mapParam")
|
||||
public class ArticleViewerWithMapParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable Map<String, String> pathVarsMap) {
|
||||
|
||||
String articleId = pathVarsMap.get("id");
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(Integer.valueOf(articleId));
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.web.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/optionalParam")
|
||||
public class ArticleViewerWithOptionalParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Optional<Integer> optionalArticleId) {
|
||||
|
||||
if(optionalArticleId.isPresent()) {
|
||||
Integer articleId = optionalArticleId.get();
|
||||
return new Article(articleId);
|
||||
}else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.web.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/requiredAttribute")
|
||||
public class ArticleViewerWithRequiredAttributeController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id", required = false) Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.web.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/seperateMethods")
|
||||
public class ArticleViewerWithTwoSeparateMethodsController {
|
||||
|
||||
@RequestMapping(value = "/article/{id}")
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
return new Article(articleId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/article")
|
||||
public Article getDefaultArticle() {
|
||||
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user