spring controller does not work when I extend it - spring

i try make a generic controller with some methods, so i don´t need re-writer common codes, but don´t work, why???
#Controller("/home/teste")
public class CtrlTeste extends ControladorGenericoSpring<Assistenciado>
{
public String path;
public CtrlTeste()
{
super(Assistenciado.class);
path = "/home/teste";
setPacoteServico("servico.assistenciado");
setPrefixo("Serv");
setNomeEntidade("Assistênciado");
}
#RequestMapping
public String teste(#RequestParam(value = "id", required= true)Long id, Model model)
{
Assistenciado ass = getServico().buscarPorId(id);
model.addAttribute("assistenciado", ass);
return "/home";
}
}

You're currently specifying the name of the bean. Try with:
#Controller
#RequestMapping("/home/teste")
public class CtrlTeste extends ControladorGenericoSpring<Assistenciado> {
...
}

Related

Abstract example in spring

Best code practice for not duplicate your code in spring controller by abstract.
I've got two controllers for example
#Controller
public class DoSomethingController {
private Entity helpfulMethod(Form form) {
Entity e = new Entity();
return e;
}
#PostMapping("/something")
public String something(Form form){
helpfulMethod(form);
}
}
#Controller
public class DoSomethingElseController {
private Entity helpfulMethod(Form form) {
Entity e = new Entity();
return e;
}
#PostMapping("/somethingElse")
public String somethingElse(Form form){
helpfulMethod(form);
}
}
How to take helpfulMethod out and connect them from outside using abstract?
I guess you need to introduce a super class for both the controllers
public abstract class BaseDoSomethingController {
protected Entity helpfulMethod(Form form) {
Entity e = new Entity();
return e;
}
}
and let both your controllers inherit the base class
#Controller
public class DoSomethingController extends BaseDoSomethingController {
#PostMapping("/something")
public String something(Form form){
helpfulMethod(form);
}
}
and the same for the second controller

MVC - break controller to subclass hierarchy

I currently have a single controller (implemented using Spring mvc), for students and teachers to upload files Controller name is FileUpoadController
I would like to break the controller functionality and extend it using:
StudentFileUploadController extends FileUpoadController
LecturerFileUpoadController extends FileUpoadController
So FileUpoadController will be abstract and maintain the base functionality.
Current controller:
#Controller
#RequestMapping(value = "/upload")
public class FileUpoadController
One solution is to make different upload mapping with two controllers
#RequestMapping(value = "/uploadStudent")
#RequestMapping(value = "/uploadLecturer")
Is it possible any other way?
It is very unlikely that those method behave exactly the same besides the different mapping. But you can use delegation instead:
public class FileUpoadController<T> {
public List<T> getList(){
// returns list of T
}
}
#Controller(value = "/uploadStudent")
public class UploadStudentController extends FileUpoadController<UploadStudent>{
#RequestMapping(method = RequestMethod.GET, value = "/list")
public #ResponseBody List<UploadStudent> getStudent() {
return super.getList();
}
}
#Controller(value = "/uploadLecturer")
public class UploadLecturerController extends FileUpoadController<UploadLecture>{
#RequestMapping(method = RequestMethod.GET, value = "/list")
public #ResponseBody List<UploadLecture> getLecture() {
return super.getList();
}
}
for more details please refer this:
https://www.codeproject.com/Articles/799677/The-Hierarchy-of-Controller-Class-in-ASP-NET-MVC

Inherited methods in Spring-Data-Neo4j repository interfaces not working

I have an abstract domain class containing a uid field, looking as below:
public abstract class GraphEntityWithUid extends GraphEntity {
private String uid = CommonUtils.newUid();
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
And, an abstract repository for it:
public abstract interface GraphEntityWithUidRepository<T extends GraphEntityWithUid> extends GraphRepository<T> {
public T findByUid(String uid);
}
I have a concrete domain class that inherits the uid, looking as below:
#NodeEntity
public class Attachment extends GraphEntityWithUid {
...
}
And, its repository looks as below:
public interface AttachmentRepository extends GraphEntityWithUidRepository<Attachment> {
}
Now, when I use the findByUid method as below:
// returns null
attachmentRepository.findByUid(uid);
it always returns null. However, if I re-declare the method in the AttachmentRepository as below, it works properly:
public interface AttachmentRepository extends GraphEntityWithUidRepository<Attachment> {
// Shouldn't this be automatically inherited??
public Attachment findByUid(String uid);
}
Why should I need to re-declare findByUid method in AttachmentRepository? Shouldn't it be automatically inherited from GraphEntityWithUidRepository?

Spring Boot validation with Hibernate Validator

Anyone know if it`s possible to create one method in my entity, to execute when I put the annotation #Valid in my class?
Example:
I have this Object:
public class Area {
#NotEmpty
private String unidade;
#NotNull
private double tamanho;
public String getUnidade() {
return unidade;
}
public void setUnidade(String unidade) {
this.unidade = unidade;
}
public double getTamanho() {
return tamanho;
}
public void setTamanho(double tamanho) {
this.tamanho = tamanho;
}
}
And I have this method:
#RestController
#RequestMapping("/recolhimento")
public class RecolhimentoController {
#RequestMapping(method = RequestMethod.GET)
public boolean getRecolhimento(#Valid Area area){
...
}
}
so when I call this method the Spring Boot will validate my model Area( but I want to create one method that will be execute when I use #Valid.
it`s possible? how?
Yes, it is possible.
You can find examples in this project: https://github.com/malkusch/validation

Spring MVC Using #RequestMapping on controller class with variable

I would like to write whole controller to work with entity.
I would like to declare the id of entity on class level and use it on each method. Here is the controller class:
#Controller
#RequestMapping(value="/job/{j_id}/instance")
public class JobController extends GenericController {
private final String htmlDir = "job/";
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable Long instance_id) {
ModelAndView result = new ModelAndView(htmlDir + "instance");
result.addObject("instance_id", instance_id);
Here I would like to use variable j_id from #RequestMapping
return result;
}
}
Can I achive this? Please help. Give me some code snippest please.
Have a try like this
#Controller
#RequestMapping(value="/job/{j_id}/instance")
public class JobController {
private final String htmlDir = "job/";
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable(value="j_id") Long instance_id) {
ModelAndView result = new ModelAndView(htmlDir + "instance");
result.addObject("instance_id", instance_id);
System.out.println("Instance Id -------------> " + instance_id);
return result;
}
}
Please notic "#PathVariable(value="j_id")"
To get both variables, you can change that line as following:
#RequestMapping(value="{i_id}/open", method=RequestMethod.GET)
public ModelAndView open(#PathVariable(value="j_id") Long jnstance_id, #PathVariable(value="i_id") Long instance_id) {
.....
}

Resources