【问题标题】:SpringBoot Xml Viewing doesnt work Even Installed Annotaitons即使安装了注释,Spring Boot Xml 查看也不起作用
【发布时间】:2021-11-29 10:54:38
【问题描述】:

虽然我应用了 XML 注释,但在查询时无法获得 Xml 格式的结果。你能帮帮我吗?

虽然我应用了 XML 注释,但在查询时无法获得 Xml 格式的结果。你能帮忙吗?

我也在使用json插件,我在隐藏的chrome页面中打开了这个结果

POM.XML

<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.javaegitimleri</groupId>
  <artifactId>petclinic</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.1.RELEASE</version>
  </parent>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-library</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <optional>true</optional>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <optional>true</optional>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
          <version>2.1</version>
      </dependency>
  </dependencies>
  <properties>
      <java.version>1.8</java.version>
  </properties>
  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>
</project>

Owner.java

包 com.javaegitimleri.petclinic.model;

    import java.util.HashSet;
    import java.util.Set;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    @XmlRootElement
    public class Owner {
      private Long id;
      private String firstName;
      private String lastName;
      
      private Set<Pet> pets = new HashSet<>();
    
      public Long getId() {
          return id;
      }
    
      public void setId(long id) {
          this.id = id;
      }
    
      public String getFirstName() {
          return firstName;
      }
    
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
    
      public String getLastName() {
          return lastName;
      }
    
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
    
      @XmlTransient
      @JsonIgnore
      public Set<Pet> getPets() {
          return pets;
      }
    
      public void setPets(Set<Pet> pets) {
          this.pets = pets;
      }
    
      @Override
      public String toString() {
          return "Owner [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
      }
      
      
      
      
    }

应用程序属性

management.endpoints.enabled-by-default=false

petclinic.display-owners-with-pets=true

spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

spring.resources.chain.strategy.content.enabled=true
spring.thymeleaf.enabled=false
spring.mvc.contentnegotiation.favor-path-extension=true
spring.mvc.pathmatch.use-suffix-pattern=true

结果

新结果

【问题讨论】:

标签: java rest spring-mvc jaxb jax-rs


【解决方案1】:

我通过网站搜索解决了!

我们应该将这些代码添加到 POM.XML

  <dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
  </dependency>

  <dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
  </dependency>

Source of the solution link!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 2019-09-18
    • 2019-12-18
    • 2017-05-22
    • 2021-08-01
    • 2023-04-06
    • 2016-09-27
    相关资源
    最近更新 更多