我是 Springboot 新手,当我使用 Postman 进行测试时,我收到此错误:没有为类 [Ldsawmain.entity.vo.TagVO; 找到主要或单个唯一构造函数;
SpringBoot版本:3.1.2
这里是班级
翻译控制器
package dsawmain.controller;
import com.mysql.cj.util.StringUtils;
import dsawmain.common.jsr303Group.DefaultGroup;
import dsawmain.common.jsr303Group.IgnoreDetailGroup;
import dsawmain.common.response.ErrorInfo;
import dsawmain.common.response.Result;
import dsawmain.common.response.StatusCode;
import dsawmain.common.utils.AuthUtils;
import dsawmain.common.utils.FuzzyQueryUtils;
import dsawmain.common.utils.RegexUtils;
import dsawmain.entity.bo.TranslationBO;
import dsawmain.entity.dto.TranslDetailDTO;
import dsawmain.entity.mysql.Tag;
import dsawmain.entity.mysql.Translation;
import dsawmain.entity.mysql.User;
import dsawmain.entity.vo.TagVO;
import dsawmain.entity.vo.TranslationVO;
import dsawmain.entity.vo.WordVO;
import dsawmain.service.serviceInterface.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@io.swagger.v3.oas.annotations.tags.Tag(name = "TranslationController",
description = "翻译管理")
@RestController
@RequestMapping("/api")
public class TranslationController {
@Value("${constant.like-thresholds}")
private int likeThresholds;
@Value("${constant.default-contributor}")
private String defaultContributor;
private final WordService wordService;
private final TranslationService translationService;
private final TagService tagService;
private final RegexUtils regexUtils;
private final FuzzyQueryUtils fuzzyQueryUtils;
private final FuzzyWordService fuzzyWordService;
private final AuthUtils authUtils;
private final UserService userService;
@Autowired
public TranslationController(WordService wordService, TranslationService translationService,
TagService tagService, RegexUtils regexUtils,
FuzzyQueryUtils fuzzyQueryUtils, FuzzyWordService fuzzyWordService,
AuthUtils authUtils, UserService userService) {
this.wordService = wordService;
this.translationService = translationService;
this.tagService = tagService;
this.regexUtils = regexUtils;
this.fuzzyQueryUtils = fuzzyQueryUtils;
this.fuzzyWordService = fuzzyWordService;
this.authUtils = authUtils;
this.userService = userService;
}
@Operation(summary = "addTranslation", description = "添加待翻译词的释义")
@Parameters({@Parameter(name = "Translation", description = "释义"),
@Parameter(name = "Word", description = "待翻译词"),
@Parameter(name = "Tags", description = "标签分类"),
@Parameter(name = "HttpRequest", description = "Http请求")})
@PostMapping("/add_translation")
public Result addTranslation(@Valid TranslationVO translationVO,
@Valid WordVO wordVO,
@Validated(IgnoreDetailGroup.class) TagVO[] tagVOS,
HttpServletRequest request){
String translation = regexUtils.removeExtraSpace(translationVO.getTranslation());
String word = regexUtils.removeExtraSpace(wordVO.getWord());
if (StringUtils.isNullOrEmpty(translation) || StringUtils.isNullOrEmpty(word)){
return Result.err(new ErrorInfo(StatusCode.INVALID_PARAMETER.getResultMsg(),
StatusCode.NOT_FOUND.getResultCode()));
}
HashSet<String> tags = new HashSet<>();
if (!ObjectUtils.isEmpty(tagVOS)){
for (TagVO tagVO : tagVOS) {
String tag = tagVO.getTag();
tag = regexUtils.removeExtraSpace(tag);
if (StringUtils.isNullOrEmpty(tag)){
continue;
}
tags.add(tag);
}
}
TranslationBO translationBO = new TranslationBO();
translationBO.setTranslation(translation);
translationBO.setWord(word);
translationBO.setLikes(1);
translationBO.setDeleted(false);
translationBO.setDate(new Date());
translationBO.setFuzzyWord(fuzzyQueryUtils.hanzi2Pinyin(translation));
translationBO.setTags(new ArrayList<>(tags));
translationBO.setContributor(defaultContributor);
// translationBO.setDetail(translationVO.getDetail());
String token = authUtils.getTokenFromRequest(request);
String uuid = authUtils.getUUIDFromToken(token);
if(uuid != null){
User user = userService.queryUser(uuid);
if (!ObjectUtils.isEmpty(user)){
translationBO.setContributor(user.getUsername());
}
}
ArrayList<TranslDetailDTO> details = new ArrayList<>();
translationBO.setDetails(details);
if (!StringUtils.isNullOrEmpty(translationVO.getDetail())){
TranslDetailDTO detailDTO = new TranslDetailDTO();
detailDTO.setDetail(translationVO.getDetail());
detailDTO.setDate(new Date());
detailDTO.setContributor(defaultContributor);
detailDTO.setContributor(translationBO.getContributor());
details.add(detailDTO);
}
Translation returnTranslation = null;
try{
returnTranslation = translationService.addTranslation(translationBO);
}catch (Exception e){
return Result.err(new ErrorInfo(StatusCode.ERROR.getResultMsg(), StatusCode.ERROR.getResultCode()));
}
return Result.suc(returnTranslation, "添加成功");
}
@Operation(summary = "queryTranslation", description = "查找符合条件的释义")
@GetMapping("/query_translation")
public Result queryTranslation(@Valid @RequestBody @Parameter(name = "Word", description = "待翻译词") WordVO wordVO){
String word = regexUtils.removeExtraSpace(wordVO.getWord());
if (StringUtils.isNullOrEmpty(word)){
return Result.err(new ErrorInfo(StatusCode.INVALID_PARAMETER.getResultMsg(),
StatusCode.INVALID_PARAMETER.getResultCode()));
}
List<TranslationBO> translations = null;
try {
translations = translationService.findTranslations(word, likeThresholds);
}catch (Exception e){
return Result.err(new ErrorInfo(StatusCode.ERROR.getResultMsg(), StatusCode.ERROR.getResultCode()));
}
return Result.suc(translations, "请求成功");
}
标签VO
package dsawmain.entity.vo;
import dsawmain.common.jsr303Group.DefaultGroup;
import dsawmain.common.jsr303Group.IgnoreDetailGroup;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TagVO {
// @NotNull(message = "数据不能为空")
@Size(max = 100, min = 1, message = "数据长度不合法", groups = {DefaultGroup.class, IgnoreDetailGroup.class})
private String tag;
@Size(max = 1000, min = 1, message = "数据长度不合法", groups = {DefaultGroup.class})
private String tagDetail;
}
WordVO
package dsawmain.entity.vo;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class WordVO {
@NotNull(message = "数据不能为空")
@Size(max = 100, min = 1, message = "数据长度不合法")
private String word;
}
错误日志
java.lang.IllegalStateException: No primary or single unique constructor found for class [Ldsawmain.entity.vo.TagVO;
at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:268) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:221) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:149) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.11.jar:6.0.11]
我对这个错误感到困惑,我已经搜索过它但找不到解决它的方法:(
我尝试过@ModelAttribute注解但无法解决这个问题 有人可以给我一些建议吗?
您正在邮递员中传递查询参数,但我没有看到注释@RequestParam。根据您的邮递员请求,它应该是 @RequestParam("word") String wordVO 等,或者如果您需要 @Valid 和对象 WordVO,则使用 @RequestBody @Valid 并发送 json。