简体   繁体   中英

Which pattern of getting infromation in Spring Boot Application @RestController is better and how to specify ANY @Mapping?

I have @RestController in Spring Boot Application.

@PutMapping("{table}/{key}")
    public void update(@PathVariable("tables") String tableName,
                               @PathVariable("key") String key,
                               @RequestBody Entity entity) {
    ... 
    }

It's more convinient to transform this mapping into:

@PutMapping("{table}/{key}")
        public void update(@RequestBody Entity entity) {
        ... 
        }

and expect that information from @PathVariable would be inside @RequestBody .

Reason : I have another @RestController in which I recieve AnotherEntity anotherEntity . This architecture style permit me to create generic hierarchy in service layers

Question 1 : Isn't a bad pattern? Is it quite good as perfect REST service or I should avoid It? Question 2 : In this case I don't use @PathVariable and I just need to specify someWord/SomeOtherWord as @PutMapping path . Is there some way to specify something like any/any without intellij inspection that I should use It?

Update: an example of architecture

public abstract class Validator<T> {
    public abstract void validate(T t);
}

public class FirstEntityValidator extends Validator<FirstEntity> {
     public void validate(FirstEntity entity){
     ...
     }
}

public class SecondEntityValidator extends Validator<SecondEntity> {
     public void validate(SecondEntity entity){
     ...
     }
}

public abstract class EntityService<T> {

    private Validator<T> validator;

    public EntityService(Validator<T> validator){
      this.validator = validator;
    }
}

public class FirstEntityService extends EntityService<FirstEntity> {

    public FirstEntityService(FirstEntityValidator<FirstEntity> validator){
       super(validator);
    }
}

public class SecondEntityService extends EntityService<SecondEntity> {

    public SecondEntityService(SecondEntityValidator<SecondEntity> validator){
       super(validator);
    }
}

I would avoid something like this. You should separate your REST Controllers like you separate your Services. In case that you need to change something for just one entity you need to refactor everything anyway or produce some ugly code.

I think what you are trying to do is to expose your Repositories as a REST Webservice. Spring can handle this for you. Check this out: https://docs.spring.io/spring-data/rest/docs/current/reference/html/

Use interfaces. Don't use any abstract classes. It is not necessary and can be very difficult to handle in bigger projects

This would be better:

public interface class Validator<T> {
  void validate(T t);
}

public class FirstEntityValidator implements Validator<FirstEntity> {
   public void validate(FirstEntity entity){
   ....
   }
}


public class SecondEntityValidator implements Validator<SecondEntity> {
   public void validate(SecondEntity entity){
   ....
   }
}

public interface EntityService{

 //your methods....

}

public class FirstEntityService implements EntityService{

    private FirstEntityValidator validator;

    public FirstEntityService(FirstEntityValidator validator){
        this.validator = validator;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM