警告:HTTP GET 方法 public java.lang.String 不应使用任何表单参数(已更新)
WARNING: A HTTP GET method, public java.lang.String, should not consume any form parameter(UPDATED)
我在 Postman 中执行 getCustomers() 方法时遇到了这个错误。问题是由于某种原因执行方法 getOrder() 而不是 getCustomers(),如果我让它取消注释。我已经搜索过这个问题,但我找不到任何适合我的案例。任何帮助将不胜感激。
错误:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A HTTP GET method, public java.lang.String com.something.JerseyProjectConnectionToDB.App.getOrder(java.lang.String), should not consume any form parameter.; source='ResourceMethod{httpMethod=GET, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.something.JerseyProjectConnectionToDB.App, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@3ea75b05]}, definitionMethod=public java.lang.String com.something.JerseyProjectConnectionToDB.App.getOrder(java.lang.String), parameters=[Parameter [type=class java.lang.String, source=date, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:371)
at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize(ApplicationHandler.java:293)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:292)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4902)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5206)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:843)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:433)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:772)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:342)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)
App.java
@Path("/app")
public class App {
@GET
@Path("/getCustomers")
@Produces(MediaType.APPLICATION_JSON)
public List<CustomerPOJO> getCustomers() {
return CustomerDAO.getAllCustomers();
}
/*
@GET
@Path("/getOrder")
@Produces(MediaType.APPLICATION_JSON)
public String getOrder(@FormParam("date") String date){
return CustomerDAO.getOrder(date);
}
*/
}
CustomerDAO.java
public class CustomerDAO {
public static List<CustomerPOJO> getAllCustomers(){
List<CustomerPOJO> customerList = new ArrayList<CustomerPOJO>();
CustomerPOJO customer = null;
try(Connection conn = DBConnection.createNewDBconnection()){
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * from customer");
while(rs.next()) {
customer = new CustomerPOJO();
customer.setCustomer_code(rs.getInt(1));
customer.setCustomer_name(rs.getString(2));
customer.setCustomer_adress(rs.getString(3));
customer.setCustomer_age(rs.getInt(4));
customerList.add(customer);
}
rs.close();
stmt.close();
} catch (SQLException e) {
logger.error(e);
}
return customerList;
}
public static String getOrder(String pattern){
List<OrderShipmentPOJO> orderList = new ArrayList<OrderShipmentPOJO>();
OrderShipmentPOJO order = null;
ObjectMapper objectMapper = new ObjectMapper();
String orderListToString = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd");
Date date = null;
try(Connection conn = DBConnection.createNewDBconnection()){
date = simpleDateFormat.parse(pattern);
logger.info(date);
if(CheckDate.execute(date)) {
String query = "select customer.customer_name, product.product_title, order_shipment.order_date\r\n"
+ "from customer, product, order_shipment, order_shipment_has_product\r\n"
+ "where order_shipment.order_code = order_shipment_has_product.order_code \r\n"
+ "and (order_shipment_has_product.product_code = product.product_code\r\n"
+ "and (order_shipment.customer_code = customer.customer_code\r\n"
+ "and (date(order_shipment.order_date) = '?')))";
PreparedStatement ps = conn.prepareStatement(query);
ps.setDate(1, (java.sql.Date) date);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
order = new OrderShipmentPOJO();
order.setCustomer_name(rs.getString(1));
order.setProduct_title(rs.getString(2));
order.setOrder_date(rs.getDate(3));
orderList.add(order);
}
rs.close();
} else {
return "You have inserted a date that dont exist, try again";
}
orderListToString = objectMapper.writeValueAsString(orderList);
} catch (SQLException e) {
logger.error(e);
} catch (ParseException e) {
logger.error(e);
return "Unparseable using " + date + ". Use a format like this one: 2020-11-30";
} catch (JsonProcessingException e) {
logger.error(e);
}
return orderListToString;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.something.JerseyProjectConnectionToDB</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.something</groupId>
<artifactId>JerseyProjectConnectionToDB</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyProjectConnectionToDB</name>
<build>
<finalName>JerseyProjectConnectionToDB</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.29.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.29.1</version>
</dependency>
</dependencies>
</project>
我发现问题出在 getOrder() 方法上,所以我对其进行了评论,现在可以使用了。我现在的问题是,当我取消注释该方法并调用其他方法时,代码执行方法 getOrder() 而不是我调用的方法,这可能是什么问题?
感谢您的帮助和时间^^
我找到了解决这个问题的方法。方法@GET
不接受@FormParam
,你只需要使用@QueryParam
或@PathParam
。更多信息:
我在 Postman 中执行 getCustomers() 方法时遇到了这个错误。问题是由于某种原因执行方法 getOrder() 而不是 getCustomers(),如果我让它取消注释。我已经搜索过这个问题,但我找不到任何适合我的案例。任何帮助将不胜感激。
错误:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A HTTP GET method, public java.lang.String com.something.JerseyProjectConnectionToDB.App.getOrder(java.lang.String), should not consume any form parameter.; source='ResourceMethod{httpMethod=GET, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.something.JerseyProjectConnectionToDB.App, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@3ea75b05]}, definitionMethod=public java.lang.String com.something.JerseyProjectConnectionToDB.App.getOrder(java.lang.String), parameters=[Parameter [type=class java.lang.String, source=date, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:371)
at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize(ApplicationHandler.java:293)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:292)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4902)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5206)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:843)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:433)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:772)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:342)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)
App.java
@Path("/app")
public class App {
@GET
@Path("/getCustomers")
@Produces(MediaType.APPLICATION_JSON)
public List<CustomerPOJO> getCustomers() {
return CustomerDAO.getAllCustomers();
}
/*
@GET
@Path("/getOrder")
@Produces(MediaType.APPLICATION_JSON)
public String getOrder(@FormParam("date") String date){
return CustomerDAO.getOrder(date);
}
*/
}
CustomerDAO.java
public class CustomerDAO {
public static List<CustomerPOJO> getAllCustomers(){
List<CustomerPOJO> customerList = new ArrayList<CustomerPOJO>();
CustomerPOJO customer = null;
try(Connection conn = DBConnection.createNewDBconnection()){
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * from customer");
while(rs.next()) {
customer = new CustomerPOJO();
customer.setCustomer_code(rs.getInt(1));
customer.setCustomer_name(rs.getString(2));
customer.setCustomer_adress(rs.getString(3));
customer.setCustomer_age(rs.getInt(4));
customerList.add(customer);
}
rs.close();
stmt.close();
} catch (SQLException e) {
logger.error(e);
}
return customerList;
}
public static String getOrder(String pattern){
List<OrderShipmentPOJO> orderList = new ArrayList<OrderShipmentPOJO>();
OrderShipmentPOJO order = null;
ObjectMapper objectMapper = new ObjectMapper();
String orderListToString = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd");
Date date = null;
try(Connection conn = DBConnection.createNewDBconnection()){
date = simpleDateFormat.parse(pattern);
logger.info(date);
if(CheckDate.execute(date)) {
String query = "select customer.customer_name, product.product_title, order_shipment.order_date\r\n"
+ "from customer, product, order_shipment, order_shipment_has_product\r\n"
+ "where order_shipment.order_code = order_shipment_has_product.order_code \r\n"
+ "and (order_shipment_has_product.product_code = product.product_code\r\n"
+ "and (order_shipment.customer_code = customer.customer_code\r\n"
+ "and (date(order_shipment.order_date) = '?')))";
PreparedStatement ps = conn.prepareStatement(query);
ps.setDate(1, (java.sql.Date) date);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
order = new OrderShipmentPOJO();
order.setCustomer_name(rs.getString(1));
order.setProduct_title(rs.getString(2));
order.setOrder_date(rs.getDate(3));
orderList.add(order);
}
rs.close();
} else {
return "You have inserted a date that dont exist, try again";
}
orderListToString = objectMapper.writeValueAsString(orderList);
} catch (SQLException e) {
logger.error(e);
} catch (ParseException e) {
logger.error(e);
return "Unparseable using " + date + ". Use a format like this one: 2020-11-30";
} catch (JsonProcessingException e) {
logger.error(e);
}
return orderListToString;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.something.JerseyProjectConnectionToDB</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.something</groupId>
<artifactId>JerseyProjectConnectionToDB</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyProjectConnectionToDB</name>
<build>
<finalName>JerseyProjectConnectionToDB</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.29.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.29.1</version>
</dependency>
</dependencies>
</project>
我发现问题出在 getOrder() 方法上,所以我对其进行了评论,现在可以使用了。我现在的问题是,当我取消注释该方法并调用其他方法时,代码执行方法 getOrder() 而不是我调用的方法,这可能是什么问题?
感谢您的帮助和时间^^
我找到了解决这个问题的方法。方法@GET
不接受@FormParam
,你只需要使用@QueryParam
或@PathParam
。更多信息: