【问题标题】:Why controller code not getting executed in spring boot application [duplicate]为什么控制器代码没有在 Spring Boot 应用程序中执行 [重复]
【发布时间】:2019-11-18 02:19:52
【问题描述】:

我正在尝试在STS 3 中创建一个简单的Spring Boot application,用户可以在其中注册并创建他们的帐户。我正在使用PostgreSQL 数据库和JPA 与休眠。以下是代码。

实体类

package tv.app.user;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class UserAccount {
    @Id
    @GeneratedValue
    private Long uid;

    private String username;
    private String email;
    private String password;

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

控制器

package tv.app.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserAccountController {

    @Autowired
    UserAccountService userAccountService;

    @RequestMapping(value="/register", method=RequestMethod.POST)
    public void registerAccount(@RequestBody UserAccount user) {
        System.out.println("------2------");
        System.out.println(user.getfName());
        userAccountService.register(user);
    }
}

存储库

package tv.app.user;

import org.springframework.data.repository.CrudRepository;

public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {

}

商业服务类

package tv.app.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserAccountService {

    @Autowired
    private UserAccountRepository userRepository;

    public void register(UserAccount user) {
        userRepository.save(user);
    }
}

Jsp 表单

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="/register" method="post">
        <input type="text" name="username" placeholder="Username" required /><br>
        <input type="text" name="email" placeholder="Email ID" required /><br>
        <input type="password" name="password" placeholder="Password" required /><br>
        <input type="submit" value="Signup" />
    </form>
</body>
</html>

application.properties 文件:

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.hibernate.ddl-auto=create

spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/myApp
spring.datasource.username=postgres
spring.datasource.password=postgres

似乎点击提交后,控件永远不会进入控制器,因为即使表单中的动作与请求映射相同,---2---也永远不会输出到控制台。

显示的错误是:

出现意外错误(类型=不支持的媒体类型,状态=415)。 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8”

【问题讨论】:

  • 您是否尝试过为您的请求映射注释设置consumes 属性以指示您接受哪些媒体类型?
  • 不,我不知道。你能解释一下吗。 @迈克
  • 尝试将consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE 添加到您的@RequestMapping 注释中。
  • 试过但没用。 @迈克
  • 还是 415 响应?

标签: java spring spring-boot jsp


【解决方案1】:

确保您调用的是控制器中声明的确切端点 (localhost:port/register)。如果您没有正确调用,则可能会输出 HTTP 404 错误...检查一下。

如果您没有收到任何 HTTP 错误,我会将注释更改为这种格式(并将调用相应地更改为 /register/user)(如 Spring 指导 here):

@RestController
@RequestMapping("/register")
public class UserAccountController {

@Autowired
UserAccountService userAccountService;

@PostMapping("/user")
public void registerAccount(@RequestBody UserAccount user) {
    System.out.println("------2------");
    System.out.println(user.getfName());
    userAccountService.register(user);
}
}

【讨论】:

  • 显示的错误类似于:出现意外错误(类型=不支持的媒体类型,状态=415)。不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8”
  • 你从哪里调用你的控制器?确保您正在进行 POST 调用并尝试将您的标头值声明为:application/json
猜你喜欢
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 2022-01-05
  • 2023-02-10
  • 2021-06-27
  • 1970-01-01
  • 2016-08-29
  • 2019-08-12
相关资源
最近更新 更多