学习springcloud时异常-java: 找不到符号 符号: 方法 getPname()
学习springcloud时,遇到获取实体类属性值时出现异常。
项目目前分为两个子模块,一个是实体类模块,另一个是应用层。
在查询数据后,打印pname属性时报错,代码排查一遍没有问题。
问题最后定位在pom.xml文件的打包方式。
实体类的子模块,采用的打包方式是pom方式,导致出现了问题;修改为jar或war方式就正常了
源码
父项目
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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>springcloud2</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>product-domain</module><module>product-server</module></modules><name>springcloud2 Maven Webapp</name><url>http://maven.apache.org</url><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.3.2.RELEASE</version></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-cloud.version>Hoxton.SR8</spring-cloud.version><spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><scope>import</scope><type>pom</type></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><scope>import</scope><type>pom</type></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>springcloud2</finalName></build></project>
实体类模块
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/maven-v4_0_0.xsd"><parent><artifactId>springcloud2</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product-domain</artifactId><packaging>jar</packaging><name>product-domain Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>product-domain</finalName></build></project>
package cn.wolfcode.domain.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Data
@Entity(name = "t_shop_product")
@NoArgsConstructor
@AllArgsConstructor
public class Product {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long pid;private String pname;private Double pprice;private Integer stock;}
应用层模块
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/maven-v4_0_0.xsd"><parent><artifactId>springcloud2</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product-server</artifactId><packaging>jar</packaging><name>product-server Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency><dependency><groupId>org.example</groupId><artifactId>product-domain</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><finalName>product-server</finalName></build>
</project>
ProductController
package cn.wolfcode.domain.controller;import cn.wolfcode.domain.entity.Product;
import cn.wolfcode.domain.service.ProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
public class ProductController {@Autowiredprivate ProductService productService;@RequestMapping(value = "/product/{pid}",method = RequestMethod.GET)public Product findByPid(@PathVariable("pid") Long pid){log.info("接下来要进行{}号商品信息的查询",pid);Product product = productService.findByPid(pid);System.out.println(product.getPname());log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product));return product;}}
ProductDao
package cn.wolfcode.domain.dao;import cn.wolfcode.domain.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;public interface ProductDao extends JpaRepository<Product,Long> {
}
ProductService
package cn.wolfcode.domain.service;import cn.wolfcode.domain.entity.Product;public interface ProductService {Product findByPid(Long pid);
}
ProductServiceImpl
package cn.wolfcode.domain.service.impl;import cn.wolfcode.domain.dao.ProductDao;
import cn.wolfcode.domain.entity.Product;
import cn.wolfcode.domain.service.ProductService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Optional;@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductDao productDao;@Overridepublic Product findByPid(Long pid) {Optional<Product> byId = productDao.findById(pid);Product product = null;if (byId!=null && byId.isPresent()){System.out.println(pid);System.out.println(1111111111);product = byId.get();System.out.println(JSON.toJSONString(product));}else{System.out.println(222222222);}return product;}
}