首页 文章

使用WebApp的Spring Rest API

提问于
浏览
0

我刚刚按照本教程https://spring.io/guides/tutorials/react-and-spring-data-rest/

它真的很有趣,但我想知道如何在不使用spring boot的情况下创建相同的应用程序,我想在tomcat web容器中部署它 .

在成瘾中我还想添加一个上下文路径,该应用程序将在localhost:8080 / appName / app上运行,api将在localhost上运行:8080 / appName / web

谢谢!

1 回答

  • 1

    你可以 Spring 天mvc

    @Configuration
    @EnableWebMvc
    public class WebConfig{
       //
    }
    

    @EnableWebMvc注释做了许多有用的事情 - 特别是在REST的情况下,它检测类路径上是否存在Jackson和JAXB 2,并自动创建和注册默认的JSON和XML转换器 .

    @Controller是RESTful API的整个Web层中的中心工件 . 出于本文的目的,控制器正在建模一个简单的REST资源--Foo:

    @Controller
         @RequestMapping( value = "/foos" )
            class FooController{
               @Autowired
               IFooService service;
    
               @RequestMapping( method = RequestMethod.GET )
               @ResponseBody
               public List< Foo > findAll(){
                  return service.findAll();
               }
    
               @RequestMapping( value = "/{id}", method = RequestMethod.GET )
               @ResponseBody
               public Foo findOne( @PathVariable( "id" ) Long id ){
                  return RestPreconditions.checkFound( service.findOne( id ) );
               }
    
               @RequestMapping( method = RequestMethod.POST )
               @ResponseStatus( HttpStatus.CREATED )
               @ResponseBody
               public Long create( @RequestBody Foo resource ){
                  Preconditions.checkNotNull( resource );
                  return service.save( resource );
               }
    
               @RequestMapping( value = "/{id}", method = RequestMethod.PUT )
               @ResponseStatus( HttpStatus.OK )
               public void update( @PathVariable( "id" ) Long id, @RequestBody Foo resource ){
                  Preconditions.checkNotNull( resource );
                  RestPreconditions.checkNotNull( service.findOne( resource.getId() ) );
    //update logic
                  service.save( resource );
               }
    
               @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
               @ResponseStatus( HttpStatus.OK )
               public void delete( @PathVariable( "id" ) Long id ){
                  service.delete( id );
               }
    
            }
    

    Controller实现是非公开的 - 这是因为它不需要 . 通常,控制器是依赖链中的最后一个 - 它从Spring前端控制器(DispathcerServlet)接收HTTP请求,并简单地将它们委托给服务层 . 如果没有必须通过直接引用注入或操纵控制器的用例,那么我不希望将其声明为公共 .

    请求映射很简单 - 与任何控制器一样,映射的实际值以及HTTP方法用于确定请求的目标方法 . @RequestBody将方法的参数绑定到HTTP请求的主体,而@ResponseBody对响应和返回类型执行相同的操作 . 它们还确保使用正确的HTTP转换器对资源进行编组和解组 . 将进行内容协商以主要基于Accept标头选择将使用哪个活动转换器,尽管也可以使用其他HTTP标头来确定表示 . 这是有帮助的tutorial

    Spring Data 让我们看看 IFooService ,它在我们的控制器中用于基本 curd operation

    IFooService.java

    public interface IFooService extends CrudRepository<Foo, Long> {
    }
    

    Spring数据拯救了我们的生命,Spring Data是一个高级SpringSource项目,其目的是统一和简化对不同类型的持久性存储(关系数据库系统和NoSQL数据存储)的访问 .

    这里 IFooService 扩展了CrudRepository,我们将Foo作为实体传递,并将Long作为泛型参数中的Foo id类型传递 . 如果您将在 FooController 中看到IFooService是autowire,我们正在使用以下方法:

    findAll,findOne,save,delete

    我们没有在 IFooService 中提供这些方法的实现,实际上,这些方法来自 CrudRepository 意味着curd操作是由spring数据处理的,这节省了很多时间hereCrudRepository doc你也可以从here学到很多关于spring数据的知识

相关问题