Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么控制器代码不能在spring引导应用程序中执行_Java_Spring_Spring Boot_Jsp - Fatal编程技术网

Java 为什么控制器代码不能在spring引导应用程序中执行

Java 为什么控制器代码不能在spring引导应用程序中执行,java,spring,spring-boot,jsp,Java,Spring,Spring Boot,Jsp,我试图在sts3中创建一个简单的Spring启动应用程序,用户可以在其中注册并创建自己的帐户。我正在用hibernate使用PostgreSQL数据库和JPA。下面是代码 实体类: package tv.app.user; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class UserAccount

我试图在
sts3
中创建一个简单的
Spring启动应用程序
,用户可以在其中注册并创建自己的帐户。我正在用hibernate使用
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);
    }
}
<%@ 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>
控制器

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);
    }
}
<%@ 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>
存储库

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);
    }
}
<%@ 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>
Jsp表单

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);
    }
}
<%@ 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>
似乎在单击submit之后,控件从未进入控制器,因为即使表单中的操作与请求映射相同,也从未将--2--输出到控制台

显示的错误是:

出现意外错误(类型=不支持的媒体类型,状态=415)。 “内容类型”应用程序/x-www-form-urlencoded;字符集=UTF-8'不受支持


确保您正在调用控制器中声明的确切端点(localhost:port/register)。如果调用不正确,可能会输出HTTP 404错误…请检查该错误

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


显示的错误类似于:出现意外错误(类型=不支持的媒体类型,状态=415)。“内容类型”应用程序/x-www-form-urlencoded;charset=UTF-8'不受支持您从何处呼叫控制器?确保正在进行POST调用,并尝试将标题值声明为:application/json是否尝试为请求映射注释设置
consumes
属性以指示您接受的媒体类型?不,我不知道。你能解释一下吗@Mikery正在将
consumes=MediaType.APPLICATION\u FORM\u URLENCODED\u VALUE
添加到您的
@RequestMapping
批注中。尝试了此操作,但无效@MikeStill 415的回应?