在 Spring 引导中定位字符串字段时出错
Error locating String field in Spring Boot
我试图通过数据库 (H2) 中的 CNPJ(巴西企业纳税人登记号)查找公司,但返回错误
{
"timestamp": "2022-03-30T19:30:23.823+00:00",
"status": 404,
"error": "Not Found",
"path": "/companies/cnpj/30101554000146"
}
我尝试过其他替代方法:
http://localhost:8080/companies/cnpj/'30.101.554/0001-46', http://localhost:8080/companies/cnpj/"30.101.554/0001- 46",
但错误仍然存在。我是这样实现的:
@Entity
@Table(name = "company")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@CNPJ
private String cnpj;
//skipped
}
public interface CompanyRepository extends JpaRepository<Company,Long> {
Optional<Company> findByCnpj(String cnpj);
}
public class CompanyDTO {
private Long id;
private String name;
private String cnpj;
//skipped
}
@Service
@Transactionalpublic class CompanyService {
@Autowired
private CompanyRepository companyRepository;
@Transactional(readOnly = true)
public CompanyDTO findById(Long id) {
Company resultado = companyRepository.findById(id).get();
CompanyDTO dto = new CompanyDTO(resultado);
return dto;
}
@Transactional(readOnly = true)
public CompanyDTO findByCnpj(String cnpf) {
Optional<Company> resultado = companyRepository.findByCnpj(cnpf);
CompanyDTO dto = new CompanyDTO(resultado.get());
return dto;
}
}
@RestController
@RequestMapping(value = "/companies")public class CompanyController {
@Autowired
private CompanyService companyService;
@GetMapping(value = "/{id}")
public CompanyDTO findById(@PathVariable Long id) {
return companyService.findById(id);
}
@GetMapping(value = "/cnpj/{cnpj}")
public CompanyDTO findByCnpj(@PathVariable String cnpj) {
return companyService.findByCnpj(cnpj);
}
}
预期输出为:
[
{"id": 1,
"nome": "Company 123",
"cnpj": "30.101.554/0001-46"
}
]
更新:
我将@GetMapping(value = "/cnpj/{cnpj}") 更改为@GetMapping(value = "/cnpj/**") 并且:
@GetMapping(value = "/cnpj/**")
public CompanyDTO findByCnpj(HttpServletRequest request) {
return companyService.findByCnpj(request.getRequestURI().split(request.getContextPath() + "/cnpj/")[1]);
}
适合我!谢谢
如 here 所述,使用 spring-boot 时,带斜线的 pathParams
可能会非常棘手。这篇文章很好地解释了当您的 pathVariable
有斜杠时如何避免得到 error 404
。
我试图通过数据库 (H2) 中的 CNPJ(巴西企业纳税人登记号)查找公司,但返回错误
{
"timestamp": "2022-03-30T19:30:23.823+00:00",
"status": 404,
"error": "Not Found",
"path": "/companies/cnpj/30101554000146"
}
我尝试过其他替代方法:
http://localhost:8080/companies/cnpj/'30.101.554/0001-46', http://localhost:8080/companies/cnpj/"30.101.554/0001- 46",
但错误仍然存在。我是这样实现的:
@Entity
@Table(name = "company")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@CNPJ
private String cnpj;
//skipped
}
public interface CompanyRepository extends JpaRepository<Company,Long> {
Optional<Company> findByCnpj(String cnpj);
}
public class CompanyDTO {
private Long id;
private String name;
private String cnpj;
//skipped
}
@Service
@Transactionalpublic class CompanyService {
@Autowired
private CompanyRepository companyRepository;
@Transactional(readOnly = true)
public CompanyDTO findById(Long id) {
Company resultado = companyRepository.findById(id).get();
CompanyDTO dto = new CompanyDTO(resultado);
return dto;
}
@Transactional(readOnly = true)
public CompanyDTO findByCnpj(String cnpf) {
Optional<Company> resultado = companyRepository.findByCnpj(cnpf);
CompanyDTO dto = new CompanyDTO(resultado.get());
return dto;
}
}
@RestController
@RequestMapping(value = "/companies")public class CompanyController {
@Autowired
private CompanyService companyService;
@GetMapping(value = "/{id}")
public CompanyDTO findById(@PathVariable Long id) {
return companyService.findById(id);
}
@GetMapping(value = "/cnpj/{cnpj}")
public CompanyDTO findByCnpj(@PathVariable String cnpj) {
return companyService.findByCnpj(cnpj);
}
}
预期输出为:
[
{"id": 1,
"nome": "Company 123",
"cnpj": "30.101.554/0001-46"
}
]
更新:
我将@GetMapping(value = "/cnpj/{cnpj}") 更改为@GetMapping(value = "/cnpj/**") 并且:
@GetMapping(value = "/cnpj/**")
public CompanyDTO findByCnpj(HttpServletRequest request) {
return companyService.findByCnpj(request.getRequestURI().split(request.getContextPath() + "/cnpj/")[1]);
}
适合我!谢谢
如 here 所述,使用 spring-boot 时,带斜线的 pathParams
可能会非常棘手。这篇文章很好地解释了当您的 pathVariable
有斜杠时如何避免得到 error 404
。