即使安装了注释,Spring Boot Xml 查看也不起作用

Posted

技术标签:

【中文标题】即使安装了注释,Spring Boot Xml 查看也不起作用【英文标题】:SpringBoot Xml Viewing doesnt work Even Installed Annotaitons 【发布时间】: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

结果

新结果

【问题讨论】:

这能回答你的问题吗? javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath 【参考方案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!

【讨论】:

以上是关于即使安装了注释,Spring Boot Xml 查看也不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何将基于 java 的注释 maven 项目转换为 Spring Boot?

是否可以让Jackson ObjectMapper在Spring Boot应用程序中遵守JAXB XML注释?

spring boot2+jpa+thymeleaf增删改查例子

spring boot实现增删改查

spring boot实现增删改查

Spring Boot 注释和导入无法在 Intellij IDE 上解析