我正在尝试这个教程spring-playground用于sprint response web应用程序
我希望响应在xml中,但是当我将@XmlRootElement提供给Person类并调用http://localhost:8090/persons时 只获得存储库中第一个人的响应。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<age>41</age>
<id>58a47bf7832db7039db3a287</id>
<name>Stephine maldini</name>
</person>
但是删除@XmlRootElement将返回Json格式的所有Person [ { “id”:“58a47bf7832db7039db3a287”, “名字”:“Stephine maldini”, “年龄”:41 }, { “id”:“58a47bf8832db7039db3a288”, “名字”:“Arjen Poutsma”, “年龄”:10 }, { “id”:“58a47bf8832db7039db3a289”, “名字”:“乔什长”, “年龄”:81 }, { “id”:“58a47bf8832db7039db3a28a”, “名字”:“Rossen Stoyanchev”, “年龄”:2 }, { “id”:“58a47bf8832db7039db3a28b”, “名字”:“Sebastin Deleuse”, “年龄”:72 }, { “id”:“58a47c03832db7039ebcfbc6”, “名字”:“Stephine maldini”, “年龄”:63 }, { “id”:“58a47c03832db7039ebcfbc7”, “名字”:“Arjen Poutsma”, “年龄”:83 } ]
下面给出了类和POM
package com.playground;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.data.annotation.Id;
@Component
@XmlRootElement
@Document
public class Person {
@Id
private String id;
private String name;
private int age;
public Person(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Person ( String name, int age) {
this.name = name;
this.age= age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package com.playground;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
public interface PersonRepository extends MongoRepository<Person, String> {
@Query("{}")
Stream<Person> all();
CompletableFuture<Person> findById(String id);
}
package com.playground;
import java.util.Optional;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.Request;
import org.springframework.web.reactive.function.Response;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Component
public class PersonHandler {
private final PersonRepository personRepository;
public PersonHandler(PersonRepository personRepository) {
this.personRepository = personRepository;
}
Response<Flux<Person>> all(Request request) {
Flux<Person> flux = Flux.fromStream(this.personRepository.all());
return Response.ok().body(BodyInserters.fromPublisher(flux, Person.class));
}
Response<Mono<Person>> byId(Request request) {
Optional<String> optional = request.pathVariable("id");
return optional.map(id -> this.personRepository.findById(id))
.map(person -> Mono.fromFuture(person))
.map(mono -> Response.ok().body( BodyInserters.fromPublisher(mono, Person.class)))
.orElseThrow(() -> new IllegalStateException("Oops"));
}
}
package com.playground;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.RouterFunction;
import org.springframework.web.reactive.function.RouterFunctions;
import reactor.ipc.netty.http.server.HttpServer;
import static org.springframework.web.reactive.function.RequestPredicates.GET;
import java.io.IOException;
@SpringBootApplication
public class SpringReactiveLearninAdithiApplication {
@Bean
RouterFunction<?> router (PersonHandler handler) {
return RouterFunctions.route( GET ("/persons"), handler::all)
.and(RouterFunctions.route (GET("/persons/{id}"), handler::byId));
}
@Bean
public HttpServer startReactorServer(RouterFunction<?> router) throws InterruptedException {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(router);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(8090);
server.newHandler(adapter).block();
return server;
}
public static void main(String[] args) throws IOException {
SpringApplication.run(SpringReactiveLearninAdithiApplication.class, args);
System.out.println("Press ENTER to exit.");
System.in.read();
}
}
<?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.reactor.xample</groupId>
<artifactId>reactive-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-reactive-learnin</name>
<description> project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web-reactive</artifactId>
<version>5.0.0.M2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</dependency>
<dependency>
<groupId>org.projectreactor.spring</groupId>
<artifactId>reactor-spring-context</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-dependencies-web-reactive</artifactId>
<version>0.1.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
请让我知道我缺少什么才能获得回复&gt; all(请求请求){XML格式。
感谢您的帮助。