简体   繁体   English

实施REStful Web服务最佳实践?

[英]Implementing REStful web services best practice?

I am using Jersey Restful webservices.I have below end point. 我正在使用Jersey Restful webservices.I低于终点。

@Path("/persons")
public class PersonWS {
    private final static Logger logger = LoggerFactory.getLogger(PersonWS.class);

    @Autowired
    private PersonService personService;

    @GET
    @Path("/{id}")
    @Produces({MediaType.APPLICATION_XML})
    public Person fetchPerson(@PathParam("id") Integer id) {
        return personService.fetchPerson(id);
    }

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_XML)
    @Produces (MediaType.APPLICATION_XML)
    public Person addPerson(Person person) {
        personService.addPerson(person);
        return person;
    }
}

Above class does not implement any interface. 上面的类没有实现任何接口。 Should i write a interface first and implement the methods of the interface in PersonWS.java? 我应该先编写一个接口并在PersonWS.java中实现该接口的方法吗? Or no need of writing any interface here? 还是无需在此处编写任何接口?

Thanks! 谢谢!

The only point of having an interface is the option to provide a different implementation. 具有接口的唯一点是提供不同实现的选项。 Usually the only other implementation besides the main one is a mock used for testing. 通常,除主要实现外,唯一的其他实现是用于测试的模拟。 However, modern mocking frameworks will easily mock out your implementation class with no need for a separate interface. 但是,现代的模拟框架可以轻松地模拟出您的实现类,而无需单独的接口。

In earlier times the interfaces were also necessary for techniques like method interceptors implemented with dynamic proxies (Spring AOP), but this requirement was obsoleted by load-time weaving and other modern techniques. 在早期,接口对于使用动态代理(Spring AOP)实现的方法拦截器之类的技术也是必需的,但此要求已被加载时编织和其他现代技术所淘汰。

For these reasons I find the overhead of maintaining a duplicate of all your methods in an interface unjustified by any real benefit in modern application architectures. 由于这些原因,我发现在接口中维护所有方法的重复项的开销没有现代应用程序体系结构的任何实际好处所能证明。

The other reason to provide an interface is to allow to inject your controller in another one if needed (with spring for example) or if you want to share and distibute a contract of your application accross modules : your contract jar will contain domain and interfaces - without considering implementations and without the need to embed all your implementations in all your modules. 提供接口的另一个原因是,如果需要(例如,使用spring),或者如果您希望跨模块共享和分配应用程序的合同,则允许将控制器注入另一个控制器:您的合同jar将包含域和接口-无需考虑实现,也无需将所有实现嵌入所有模块中。

This could be consider as a good practice because it provides loose coupling and prepares evolving and growing but there is no technical need to provide an interface if you don't need it. 可以将其视为一种良好的做法,因为它提供了松散的耦合并为不断发展的应用做好了准备,但是如果您不需要它,则不需要技术来提供接口。

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

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