<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Boot Jersey 教程 > 原文: [http://zetcode.com/springboot/jersey/](http://zetcode.com/springboot/jersey/) Spring Boot Jersey 教程展示了如何在 Spring Boot 應用中使用 Jersey 建立一個簡單的 RESTFul 應用。 Jersey 是使用`@RestController`創建的 Spring RESTFul 應用的替代方案。 Spring 是用于創建企業應用的流行 Java 應用框架。 Spring Boot 是 Spring 框架發展的下一步。 它有助于以最小的努力創建獨立的,基于生產級的 Spring 應用。 它提倡在 XML 配置上使用約定而不是配置原則。 ## RESTFul 應用 RESTFul 應用遵循 REST 架構樣式,該樣式用于設計網絡應用。 RESTful 應用生成對資源執行 CRUD(創建/讀取/更新/刪除)操作的 HTTP 請求。 RESTFul 應用通常以 JSON 或 XML 格式返回數據。 ## JAX-RS RESTful Web 服務的 Java API(JAX-RS)是 Java 編程語言 API 規范,它提供了根據代表性狀態傳輸(REST)架構模式創建 Web 服務的支持。 JAX-RS 使用注解來簡化 Web 服務客戶端和端點的開發和部署。 JAX-RS 是 Java EE 的正式組成部分。 ## Jersey Jersey 是用于用 Java 開發 RESTful Web 服務的開源框架。 它是 RESTful Web 服務 Java API(JAX-RS)規范的參考實現。 ## Spring Boot `Jersey` 示例 以下應用是使用 Jersey 創建的 simpe Spring Boot RESTful 應用。 ```java $ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── config │ │ │ └── JerseyConfig.java │ │ └── endpoint │ │ ├── HelloEndpoint.java │ │ └── ReverseReturnEndpoint.java │ └── resources └── test └── java └── com └── zetcode └── ApplicationTests.java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>SpringBootJersey</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven 構建文件。 Spring Boot 啟動器是一組方便的依賴項描述符,可以極大地簡化 Maven 配置。 `spring-boot-starter-parent`具有 Spring Boot 應用的一些常用配置。 `spring-boot-starter-jersey`是使用 JAX-RS 和 Jersey 構建 RESTful Web 應用的入門。 它是`spring-boot-starter-web`的替代方法。 `spring-boot-starter-test`是使用包含 JUnit,Hamcrest 和 Mockito 的庫測試 Spring Boot 應用的入門程序。 `spring-boot-maven-plugin`在 Maven 中提供了 Spring Boot 支持,使我們可以打包可執行的 JAR 或 WAR 檔案。 它的`spring-boot:run`目標運行 Spring Boot 應用。 `application.yml` ```java server: port: 8086 context-path: /api spring: main: banner-mode: "off" logging: level: org: springframework: ERROR ``` 在`application.yml`文件中,我們編寫了 Spring Boot 應用的各種配置設置。 我們設置端口和上下文路徑。 使用`banner-mode`屬性,我們可以關閉 Spring 橫幅。 我們將 spring 框架的日志記錄級別設置為`ERROR`。 在`application.yml`文件位于中`src/main/resources`目錄。 `JerseyConfig.java` ```java package com.zetcode.config; import com.zetcode.endpoint.HelloService; import com.zetcode.endpoint.ReverseService; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.context.annotation.Configuration; @Configuration public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(HelloService.class); register(ReverseService.class); } } ``` `JerseyConfig`注冊兩個服務類別。 `HelloService.java` ```java package com.zetcode.service; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import org.springframework.stereotype.Service; @Service @Path("/hello") public class HelloService { @GET @Produces("text/plain") public String hello() { return "Hello from Spring"; } } ``` 這是`HelloService`。 `@Path`注解定義了服務類將響應的 URL。 Spring 的`@Service`也注解了`HelloService`以進行自動檢測。 我們的服務方法僅返回`"Hello from Spring"`消息。 `HelloService.java` ```java package com.zetcode.service; import javax.validation.constraints.NotNull; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.springframework.stereotype.Service; @Service @Path("/reverse") public class ReverseService { @GET @Produces("text/plain") public String reverse(@QueryParam("data") @NotNull String data) { return new StringBuilder(data).reverse().toString(); } } ``` `reverse()`服務方法返回一個反向的字符串。 它接受一個參數,不能為 null。 `@QueryParam`將 HTTP 查詢參數的值綁定到資源方法參數。 `ApplicationTests.java` ```java package com.zetcode; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ApplicationTests { @Autowired private TestRestTemplate restTemplate; @Test public void hello() { ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello from Spring"); } @Test public void reverse() { ResponseEntity<String> entity = this.restTemplate .getForEntity("/reverse?data=regit", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("tiger"); } @Test public void validation() { ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); } } ``` 在`ApplicationTests`中,我們測試了兩個端點。 `Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`設置 Spring Boot 應用。 `@SpringBootApplication`啟用自動配置和組件掃描。 ```java $ mvn spring-boot:run ``` 使用`mvn spring-boot:run`命令,運行應用。 該應用部署在嵌入式 Tomcat 服務器上。 ```java $ curl localhost:8086/api/hello Hello from Spring ``` 使用`curl`命令,我們連接到 hello 端點。 ```java $ curl localhost:8086/api/reverse?data=summer remmus ``` 夏天的字符是相反的。 在本教程中,我們使用 Jersey 借助 Spring Boot 創建了一個簡單的 RESTFul 應用,它是 JAX-RS 規范的參考實現。 您可能也對相關教程感興趣: * [Spring Boot REST XML 教程](/springboot/restxml/) * [帶有嵌入式 Jetty 的 Jersey 應用](/articles/jerseyembeddedjetty/) * [Spring Boot H2 REST 教程](/articles/springbootresth2/) * [Spring Boot Thymeleaf 教程](/articles/springbootthymeleaf/) * [Jersey 應用中的 Web URL](/articles/url/) * [Spring Web 應用介紹](/articles/springwebfirst/) * [Spring Boot RESTFul 應用](/articles/springbootrestsimple/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看