SpringBoot集成Jersey


SpringBoot集成Jersey

添加依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

添加配置

①新建JerseyConfig.java

@Component
@ApplicationPath("/jersey")
public class JerseyConfig extends ResourceConfig{

    public JerseyConfig(){
        //packages("com.example.study");
        register(UserResource.class);
    }
}

建議用register添加資源

注意ApplicationPath,如果不添加無法訪問,因為默認為/*,由於本項目無web.xml,所以在這里配置(配置文件里也可以)

②輸出的json數據格式化(方便使用,不添加也可訪問)

在application.yml中Spring塊下添加

jackson:

    default-property-inclusion: non_null
    serialization:
        indent-output: true
    date-format: yyyy-MM-dd HH:mm:ss
    parser:
        allow-missing-values: true

測試使用

新建UserResource.java

@Component
@Path("/users")
public class UserResource {

    @Autowired
    private UserMappers userMapper;


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User get(){
        List<User> users = userMapper.selectAll();
        return users.get(0);
    }

}

記得在JerseyConfig中注冊

訪問

http://localhost:8088/jersey/users

其它

注意:此時原來搭建的SpringMVC也可以訪問

SpringMVC 與jersey就一起工作了,附上項目目錄供大家參考項目異同。

這是我使用了jersey的項目,其余配置還有:

mybatis,mybatis generator
slf4+logback
thymeleaf模板引擎
alibaba的druid數據庫連接池

https://github.com/Lifan1998/study

供初學者參考。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM