gpt4 book ai didi

mysql - MVC 不返回 json...请求不可用

转载 作者:行者123 更新时间:2023-11-29 11:58:31 25 4
gpt4 key购买 nike

大家好,我想在 Spring + Mysql 中返回一个带有剩余 http 请求的用户名。

Controller :

@Controller
@SessionAttributes("user")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
public @ResponseBody String getUserName(@PathVariable String userName) {
return userName;
}
}

型号:

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue
private Long id;

@NotEmpty
@Size(min=4, max=20)
private String userName;

@NotEmpty
private String firstName;

@NotEmpty
private String lastName;

@NotEmpty
@Size(min=4, max=8)
private String password;

@NotEmpty
private String status;


@NotEmpty
@Email
private String emailAddress;


@NotNull
@Past
@DateTimeFormat(pattern="MM/dd/yyyy")
private Date dateOfBirth;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}


public String getPassword() {
return password;
}

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

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public Date getDateOfBirth() {
return dateOfBirth;
}



public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}

我只想通过将我的模型连接到我的 mysql 数据库的请求映射来检索我的服务 put、delete、post 和 get。我试图以这种方式提出请求: http://localhost:8080/portuzona/userss?userName=Arnoldo

但我明白了:

HTTP Status 404 - /portuzona/userss

type Status report

message /portuzona/userss

description The requested resource is not available.

portuzona 和 userss 已正确处理(html View ),但我无法在我的 Controller 中获取。

日志正确地表明该表已被 bean 找到:

sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: portuzona.users
sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [dateofbirth, password, firstname, emailaddress, id, username, lastname, status]

正如用户要求的那样,这里提供了更多可能有帮助的信息..或没有:

/WEB-INF/servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>usersHibernateServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>usersHibernateServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<display-name>Archetype Created Web Application</display-name>
</web-app>

/WEB-INF/config/servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.portuzona" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>

</beans>

最佳答案

看起来您混淆了映射和路径变量。两种选择:

1:

@RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
public @ResponseBody String getUname(@PathVariable String userName) {
return userName;
}

http://localhost:8080/portuzona/userss/Arnoldo

2:

@RequestMapping(method=RequestMethod.GET, value="/userss/yourmapping")
public @ResponseBody String getUserName(@RequestParam String username) {
return username;
}

http://localhost:8080/portuzona/userss/yourmapping?username=Arnoldo

关于mysql - MVC 不返回 json...请求不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32786516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com