简体   繁体   English

在Spring Boot应用程序@RestController中获取信息的哪种模式更好,以及如何指定ANY @Mapping?

[英]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. 我在Spring Boot Application中有@RestController

@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 . 并期望来自@PathVariable的信息将位于@RequestBody内

Reason : I have another @RestController in which I recieve AnotherEntity anotherEntity . 原因 :我有另一个@RestController ,我在其中接收AnotherEntity anotherEntity This architecture style permit me to create generic hierarchy in service layers 这种体系结构样式允许我在服务层中创建通用层次结构

Question 1 : Isn't a bad pattern? 问题1 :这不是坏模式吗? Is it quite good as perfect REST service or I should avoid It? 作为完善的REST服务还是相当不错,还是我应该避免使用它? Question 2 : In this case I don't use @PathVariable and I just need to specify someWord/SomeOtherWord as @PutMapping path . 问题2 :在这种情况下,我不使用@PathVariable,而只需要指定someWord / SomeOtherWord作为@PutMapping 路径 Is there some way to specify something like any/any without intellij inspection that I should use It? 有没有某种方法可以指定任何/任何东西而无需进行Intellij的检查,我应该使用它呢?

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. 您应该像分离服务那样分离REST控制器。 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. 我认为您正在尝试将存储库公开为REST Web服务。 Spring can handle this for you. Spring可以为您解决这个问题。 Check this out: https://docs.spring.io/spring-data/rest/docs/current/reference/html/ 检查一下: 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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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