当前位置: 首页 > 知识库问答 >
问题:

SpringBoot:使用REST服务发布新对象时的类型定义错误

燕承安
2023-03-14

我有一个对象,它通过json POST请求获取参数,以便在数据库中创建一个新条目,但我得到以下错误:

    {
        "email": "user@gmail.com"
    }

这是控制器

 @PostMapping("/forgot_password")
public ResponseEntity<?> processForgotPassword(@RequestBody HttpServletRequest request, Model model, User user) {
    String email = (String) request.getAttribute(user.getEmail());
    String token = RandomString.make(30);

    try {
        userService.updateResetPasswordToken(token, email);
        String resetPasswordLink = Utility.getSiteURL(request) + "/reset_password?token=" + token;
        sendEmail(email, resetPasswordLink);
        model.addAttribute("message", "We have sent a reset password link to your email. Please check.");

    } catch (ResourceNotFoundException ex) {
        model.addAttribute("error", ex.getMessage());
    } catch (UnsupportedEncodingException | MessagingException e) {
        model.addAttribute("error", "Error while sending email");
    }

    return ResponseEntity.ok("Reset link sent Successfully");
}

服务

public class UserService {

    @Autowired
    private UserRepository userRepository;


    public void updateResetPasswordToken(String token, String email) throws ResourceNotFoundException {
        User user = userRepository.findByEmail(email);
        if (user != null) {
            user.setResetPasswordToken(token);
            userRepository.save(user);
        } else {
            throw new ResourceNotFoundException("Could not find any user with the email " + email);
        }
    }

共有1个答案

慕祯
2023-03-14

下面是一个示例,HttpServletRequest注入了不需要的@RequestBody,因此只需删除该注释即可。

@PostMapping("/forgot_password")
public ResponseEntity<?> processForgotPassword(HttpServletRequest request) {
  // do something
{

@requestbody表示从HTTP请求的主体构建一个对象,但HttpServletRequest是整个HTTP请求。是不匹配导致了错误。

 类似资料:
  • 这是对象: 这是控制器方法:

  • 我有一个自定义dto类: 和一个使用Web Api(4.5. net框架)的控制器 客户端只有4.0.net框架,因此我将无法使用PostAsJsonAsync()方法。将对象从客户端传递到服务器的解决方案是什么? 我尝试了如下方法: 然而它给了我一个例外: 难道不能使用牛顿软件库吗?

  • 我已经发布了带有endpoint的JAX-WSWeb服务。在开发过程中发布。是否存在(JAX-RS中)用于在jersey中发布RESTWeb服务的此类实用程序类?我参考了几篇文章,其中大部分都是基于在一些容器中发布web服务,如Jetty、Grizzly等。

  • 问题内容: 就像我们使用__ToString一样,有没有一种方法来定义铸造方法? 问题答案: 无需在php中键入强制类型转换。 编辑: 由于这个话题似乎引起一些混乱,我想我要详细说明一下。 在Java之类的语言中,有两种可能带有类型。编译器有一个关于类型的概念,而运行时还有一个关于类型的想法。编译器的类型与变量相关,而运行时引擎则跟踪值的类型(已将其分配给变量)。变量类型在编译时是已知的,而值类型

  • 资源 ComplexObject类只是一个带有字符串的类。当我点击URL:http://localhost:8080/simple-service-webapp/webapi/myresource/This work时,我得到的是“get it!” 但是当我点击:http://localhost:8080/simple-service-webapp/webapi/myresource/comple

  • 我正在使用一个接口来定义一个新类型。这是正确的做法吗?我做如下: 然后,为了实例化它,我必须这样做: 它实际上创建了一个空对象,这不是我想要的;但是如果没有这个,它会引发一个错误,例如,“无法读取未定义的属性‘问题’”。我定义新类型的方法是错误的吗? ====EDIT====以下是我根据你的评论所做的: 然后我说: 然后我仍然得到这个错误:无法读取未定义的属性“push”。我不想在定义接口的地方启