Servlet using Controller Annotation - spring

Unable to go to Servlet from hello.jsp to MainController Class. This is a maven project.
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class MainController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "Hello";
}
#RequestMapping("displayname")
public String displayName(HttpServletRequest request) {
System.out.println("Working till here");
String firstName = request.getParameter("firstName");
request.setAttribute("firstName", firstName);
return "displayName";
}
}

#RequestMapping(value = "/displayname", method = RequestMethod.POST)
public String displayName(HttpServletRequest request) {
System.out.println("Working till here");
String firstName = request.getParameter("firstName");
request.setAttribute("firstName", firstName);
return "displayName";
}
You can change method parameter on requestmapping annotation. Also you can get body in displayName method and redirect it. I checked that method is empty when it is not defined. Maybe you can use #PostMapping annotation and other annotations like #GetMapping, #PostMapping etc...

Related

How to see result of Flux<Object> in Postman?

I mimic tutorial at https://howtodoinjava.com/spring-webflux/spring-webflux-tutorial/ . My MongoDB (My version https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.5-signed.msi). Postman v9.8.0 , Windows 10 x64.
My controller
package com.howtodoinjava.demo.controller;
import com.howtodoinjava.demo.model.Employee;
import com.howtodoinjava.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
#RestController
public class EmployeeController {
#Autowired
private EmployeeService employeeService;
#RequestMapping(value = {"/create", "/"}, method = RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
#ResponseBody
public void create(#RequestBody Employee e) {
employeeService.create(e);
}
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
#ResponseBody
public ResponseEntity<Mono<Employee>> findById(#PathVariable("id") Integer id) {
Mono<Employee> e = employeeService.findById(id);
HttpStatus status = e != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<Mono<Employee>>(e, status);
}
#RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
#ResponseBody
public Flux<Employee> findByName(#PathVariable("name") String name) {
return employeeService.findByName(name);
}
#RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
#ResponseBody
public Flux<Employee> findAll() {
// Flux<Employee> emps = employeeService.findAll();
Flux<Employee> emps = employeeService.findAll().log().delayElements(Duration.ofSeconds(1));
emps.subscribe();
return emps;
}
#RequestMapping(value = "/update", method = RequestMethod.PUT)
#ResponseStatus(HttpStatus.OK)
public Mono<Employee> update(#RequestBody Employee e) {
return employeeService.update(e);
}
#RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
#ResponseStatus(HttpStatus.OK)
public void delete(#PathVariable("id") Integer id) {
employeeService.delete(id).subscribe();
}
}
In debug console, I see the result
The reason why the responses are empty is that no getter methods are defined in the Employee entity class. Adding the following should make it work:
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getSalary() {
return salary;
}
As a side note, consider mapping your entities into EmployeeDTO instances.

DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

I am trying to design a rest api, and below is my controller code.
when i invoke http://localhost:8080/ the response is fine, but if i hit http://localhost:8080/api/ca it thorws javax.servlet.ServletException: No adapter for handler [...CaDetailController#48224381]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
#RestController("/api")
public class CaDetailController {
private static final Logger logger = LoggerFactory.getLogger(GetClassLoader.class.getClass());
#Autowired
CaService caService;
#RequestMapping(path = "/ca", method = RequestMethod.GET)
public #ResponseBody List<CaDetail> getCorporateActions() {
logger.info("CaDetailController.findAllCaDetails()");
return caService.findAllCaDetails();
}
#RequestMapping(path = "/ca/{caId}", method = RequestMethod.GET)
public #ResponseBody List<CaDetail> getCorporateActions(#PathParam("caId") long caId) {
logger.info("CaDetailController.getCorporateActions() : caId : " + caId);
return caService.findAllCaDetails();
}
}
Updated controller.
#RestController
#RequestMapping("/api/ca")
public class CaDetailController {
private static final Logger logger = LoggerFactory.getLogger(GetClassLoader.class.getClass());
#Autowired
CaService caService;
#GetMapping(path = "/")
public #ResponseBody List<CaDetail> getCorporateActions() {
logger.info("CaDetailController.findAllCaDetails()");
return caService.findAllCaDetails();
}
#GetMapping(path = "/{caId}")
public #ResponseBody List<CaDetail> getCorporateActions(#PathParam("caId") Long caId) {
logger.info("CaDetailController.getCorporateActions() : caId : " + caId);
return caService.findAllCaDetails();
}
}
For clarity, fix is:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/api/ca")
public class CaDetailController {
#GetMapping
public String healthcheck1() {
return "ok!";
}
#GetMapping(path = "health")
public String healthcheck2() {
return "ok again!";
}
}
You can call this endpoints using URL:
http://localhost:8080/api/ca and
http://localhost:8080/api/ca/health
(Assuming default Spring Boot Tomcat configuration).
Don't add ("/api") value to #RestController Annotation,
add it to #RequestMapping
#RestController
#RequestMapping("api/")
...
Try this
#RestController
#RequestMapping("/api")
public class CaDetailController {
instead of
#RestController("/api")
public class CaDetailController {

How to pass #SessionAttributes value between controllers

My first controller is Login
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.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.servlet.http.HttpSession;
import java.io.IOException;
#Controller
#SessionAttributes("session")
public class LoginController extends GlobalController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String indexAction() throws IOException {
return "login";
}
#RequestMapping(value = "/", method= RequestMethod.POST)
public String indexAction(#RequestParam String username, #RequestParam String password,HttpSession session) {
String page = "login";
if(username != "" && password != ""){
try {
if(userService.authenticationUser(username,password) == "success"){
page = "redirect:/main";
session.setAttribute("test","Salom");
//this.httpSession =session;
//System.out.println(session.getAttribute("test"));
}
else page = "login";
}
catch (Exception e){
e.fillInStackTrace();
}
}
else page = "login";
return page;
}
}
My second Controller is Test
package com.springboot.app.controllers.reports;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.servlet.http.HttpSession;
#SessionAttributes("session")
public class TestController {
#RequestMapping(value = "/test",method = RequestMethod.GET)
public String index(){
#SessionAttributes("session")HttpSession session;
return "";
}
}
---------------------------------------------------------------------------- How to pass #SessionAttributes("session") from login Controller to Test controller or how to store #SessionAttributes("session") in variable
#SessionAttributes is not intended to be used (and will not work also) to store objects in the session between different controllers. The controller annotated with #SessionAttributes must also tell that it is finished (so controllerA not ControllerB). The model message from controller a is still not available for controller B.
see this conversation
you can pass from LoginController to TestController:
e.g. Username = name
LoginController:
import org.springframework.ui.ModelMap;
#Controller
#SessionAttributes("name")
public class LoginController{
#RequestMapping(value="/login", method = RequestMethod.POST)
public String showWelcomePage(ModelMap model, #RequestParam String name, #RequestParam String password){
boolean isValidUser = service.validateUser(name, password);
if (!isValidUser) {
model.put("errorMessage", "Invalid Credentials");
return "login";
}
model.put("name", name);
model.put("password", password);
return "welcome";
TestController:
#SessionAttributes("name")
public void showUsername(ModelMap model){
System.out.println("Username is: " + (String) model.get("name");
}
}
hope there is no typo!

Spring Boot full REST CRUD example

Does anyone have a full spring boot REST CRUD example? The spring.io site just has a RequestMapping for GET. I'm able to get POST and DELETE working but not PUT.
I suspect it's how I'm trying to get the params where the disconnect is, but I have not seen an example where someone is performing an update.
I'm currently using the SO iPhone app so I can't paste my current code. Any working examples would be great!
As you can see I have implemented two way to update.
The first one will receive a json, and the second one will receive the cusotmerId in the URL and json also.
#RestController
#RequestMapping("/customer")
public class CustomerController {
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Customer greetings(#PathVariable("id") Long id) {
Customer customer = new Customer();
customer.setName("Eddu");
customer.setLastname("Melendez");
return customer;
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public List<Customer> list() {
return Collections.emptyList();
}
#RequestMapping(method = RequestMethod.POST)
public void add(#RequestBody Customer customer) {
}
#RequestMapping(method = RequestMethod.PUT)
public void update(#RequestBody Customer customer) {
}
#RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public void updateById(#PathVariable("id") Long id, #RequestBody Customer customer) {
}
#RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void delete() {
}
class Customer implements Serializable {
private String name;
private String lastname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return lastname;
}
}
}
An alternative update returns ResponseEntity object.
#RestController
#RequestMapping("/fruits")
public class FruitController {
private final Logger LOG = LoggerFactory.getLogger(FruitController.class);
#Autowired
private FruitService fruitService;
#RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<Fruit>> getAll(#RequestParam(value = "offset", defaultValue = "0") int index,
#RequestParam(value = "numberOfRecord", defaultValue = "10") int numberOfRecord) {
LOG.info("Getting all fruits with index: {}, and count: {}", index, numberOfRecord);
List<Fruit> fruits = fruitService.getAll(index, numberOfRecord);
if (fruits == null || fruits.isEmpty()) {
return new ResponseEntity<List<Fruit>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Fruit>>(fruits, HttpStatus.OK);
}
#RequestMapping(value = "{id}", method = RequestMethod.GET)
public ResponseEntity<Fruit> get(#PathVariable("id") int id) {
LOG.info("Getting fruit with id: {}", id);
Fruit fruit = fruitService.findById(id);
if (fruit == null) {
return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Fruit>(fruit, HttpStatus.OK);
}
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> create(#RequestBody Fruit fruit, UriComponentsBuilder ucBuilder) {
LOG.info("Creating fruit: {}", fruit);
if (fruitService.exists(fruit)) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
fruitService.create(fruit);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/fruit/{id}").buildAndExpand(fruit.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
#RequestMapping(value = "{id}", method = RequestMethod.PUT)
public ResponseEntity<Fruit> update(#PathVariable int id, #RequestBody Fruit fruit) {
LOG.info("Updating fruit: {}", fruit);
Fruit currentFruit = fruitService.findById(id);
if (currentFruit == null) {
return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND);
}
currentFruit.setId(fruit.getId());
currentFruit.setName(fruit.getName());
fruitService.update(fruit);
return new ResponseEntity<Fruit>(currentFruit, HttpStatus.OK);
}
#RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> delete(#PathVariable("id") int id) {
LOG.info("Deleting fruit with id: {}", id);
Fruit fruit = fruitService.findById(id);
if (fruit == null) {
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
}
fruitService.delete(id);
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
From Spring MVC RESTFul Web Service CRUD Example
I have prepared a set of tutorials on Spring Boot CRUD Operations. Following are the content of tutorials:
How to create Spring Boot Project using Spring Tool Suite
How to implement GET & POST method in spring boot restful web service
How to implement PUT & DELETE method in spring boot restful web service
Integrate PostgreSQL database using spring boot JPA with spring boot restful web service
Use of CURL commands
Youtube Tutorials:
Spring Boot Restful Web Service Tutorial | Tutorial 1 -
Introduction
Spring Boot Restful Web Services CRUD Example GET & POST | Tutorial - 2
Spring boot CRUD Operations example PUT & DELETE | Tutorial - 3
Spring Boot JPA | In 5 Simple Steps Integrate PostgreSQL Database | Tuorial - 4
Visit Blog for more details.
You can get my full RESTful server and client app using SpringBoot at Spring RESTFul Examples at github
package com.controllers;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.cats.hcm.bussinessObjects.Address;
import com.cats.hcm.bussinessObjects.Employee;
import com.cats.hcm.bussinessObjects.EmployeeBO;
import com.cats.hcm.bussinessObjects.MaritalStatus;
import com.cats.hcm.bussinessObjects.State;
import com.cats.hcm.repository.EmployeeRepositoryImpl;
import com.cats.hcm.repository.MaritalStatusRepositoryImpl;
import com.cats.hcm.repository.Repository;
import com.cats.hcm.repository.StateRepositoryImpl;
import com.cats.hcm.services.EmployeeServiceImpl;
import com.google.gson.Gson;
#Controller
#RequestMapping("/newemployee")
public class NewEmployeeController {
private static final Logger logger = LoggerFactory.getLogger(Repository.class);
#Autowired
Repository repository;
#Autowired
StateRepositoryImpl stateRepositoryImpl;
#Autowired
MaritalStatusRepositoryImpl maritalStatusRepositoryImpl;
#Autowired
EmployeeRepositoryImpl employeeRepositoryImpl;
#Autowired
EmployeeServiceImpl employeeServiceImpl;
#RequestMapping(value="/save", method=RequestMethod.POST)
public #ResponseBody String addEmployee(#RequestBody EmployeeBO employeeBO, BindingResult bindingResult) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, IOException {
logger.info("==========="+new Gson().toJson(employeeBO));
logger.info("========Employee ID========"+employeeBO.getEmployeeId());
repository.addToDataBase(employeeBO);
String msg="Success";
return msg;
}
#RequestMapping(value="/update", method=RequestMethod.POST)
public #ResponseBody String updateEmployee(#RequestBody EmployeeBO employeeBO, BindingResult bindingResult) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, IOException {
logger.info("==========="+new Gson().toJson(employeeBO));
logger.info("========Employee ID========"+employeeBO.getEmployeeId());
//Deleting Existing Employee
Boolean isDeleted = employeeServiceImpl.deleteEmployeeDetails(employeeBO.getEmployeeId());
if(isDeleted) {
repository.addToDataBase(employeeBO);
}
String msg="Success";
return msg;
}
#RequestMapping("/employeeData")
public #ResponseBody List<Employee> getEmployeeDataTablePage() {
logger.info("======getEmployeeDataTablePage======");
List<Employee> employeeList = employeeRepositoryImpl.readAll();
logger.info("========EmployeeList========="+new Gson().toJson(employeeList));
return employeeList;
}
#RequestMapping("/modifyPage")
public #ResponseBody List<Employee> getEmployeeModifyPage(#RequestParam("employeeId") String employeeId) {
logger.info("========getEmployeeModifyPage====EmployeeID:===="+employeeId);
//List<State> stateList = stateRepositoryImpl.readAll();
//List<MaritalStatus> maritalStatusList = maritalStatusRepositoryImpl.readAll();
//model.addAttribute("stateList", stateList);
//model.addAttribute("maritalStatusList", maritalStatusList);
List<Employee> employeeList = employeeRepositoryImpl.readAll();
logger.info("========employeeList:===="+employeeList);
EmployeeBO employeeBO = employeeServiceImpl.getEmployeeBO(employeeId);
logger.info("========getEmployeeModifyPage====EmployeeBO:===="+employeeBO);
return employeeList;
//return new ModelAndView("apps-mod-employee", "employee", employeeBO);
}
#RequestMapping("/delete")
public #ResponseBody String deleteEmployee(#RequestParam("employeeId") String employeeId) {
logger.info("========deleteEmployee===EmployeeID:===="+employeeId);
//employeeRepositoryImpl.delete(employeeServiceImpl.getEmployeeBO(employeeId));
Boolean isDeleted = employeeServiceImpl.deleteEmployeeDetails(employeeId);
if(isDeleted) {
logger.info("========Employee Record Deleted===EmployeeID:===="+employeeId);
}
return "redirect:/employee/employeeDataTable";
}
/*#RequestMapping("/employeeDataByEmpId")
public String getEmployeeAddPage(Map<String, Object> model) {
public ModelAndView getEmployeeDataByEmpId(ModelMap model,HttpServletRequest request, HttpServletResponse response) {
logger.info("======getEmployeeDataByEmpId======");
String EmployeeID=request.getParameter("empId");
Employee employee = employeeRepositoryImpl.read(EmployeeID);
logger.info("========Employee========="+new Gson().toJson(employee));
model.addAttribute(new EmployeeBO());
model.addAttribute("employeeByEmpId", employee);
//return "apps-add-employee";
//return new ModelAndView("apps-add-employee");
return new ModelAndView("apps-employee", "employee", new EmployeeBO());
}*/
}

How could I map "/test/test" to a method of a controller, which is mapped to "/test"?

I am confused by the behavior of Spring's #RequestMapping annotation. In the following code, test() is mapped to "/test" and test_test() is mapped to "/test/test/test". What was happening here? And what should I do if I want to map test() to "/test/test"?
package com.mvc.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping(value = "/test")
public class Test {
#RequestMapping(value = "/test", method = RequestMethod.GET)
String test() {
return "test";
}
#RequestMapping(value = "/test/test", method = RequestMethod.GET)
String test_test() {
return "test";
}
}
Spring does this intentionally; When the request mapping pattern value at method and type levels match, it uses only one of them. #See org.springframework.util.AntPathMatcher#combine()
One way is to suffix "/" to the RequestMapping value at method level (as below), so your you can use "/test/test/" as url ( NOT /test/test, ofcourse).
#Controller
#RequestMapping(value = "/test")
public class Test {
#RequestMapping(value = "/test/", method = RequestMethod.GET)
String test() {
return "test";
}
}
Don't know why this was not documented.
So, I think the only remaining way to match "/test/test" url is to use URI template patterns.
#Controller
#RequestMapping(value = "/test")
public class Test {
#RequestMapping(value = "/{anythinghere}", method = RequestMethod.GET)
String test() {
return "test";
}
}

Resources