Using Spring and Hibernate I've created a query using @Query annotation as follows:
我使用Spring和Hibernate创建了一个使用@Query注释的查询,如下所示:
@Repository
public interface SampleDAO extends CrudRepository<Sample, Long> {
@Query("select count(s) from Sample s where dna_study_id = ?1")
Integer getSamplesXAnalysis(Long id);
}
This is Sample class:
这是示例类:
@Entity
@Table(name="sample")
public class Sample implements Serializable {
@Id
@GeneratedValue
@Column(name="sample_id")
private Long sample_id;
@Column(name="id")
private String id;
@Column(name="description")
private String description;
@ManyToOne
@JoinColumn(name="dna_study_id")
private DNA_Study study;
public Sample() { }
public Sample(String id, String description, DNA_Study study) {
this.id = id;
this.description = description;
this.study = study;
}
public Long getSample_id() {
return sample_id;
}
public void setSample_id(Long sample_id) {
this.sample_id = sample_id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public DNA_Study getDna_study_id() {
return study;
}
public void setDna_study_id(DNA_Study study) {
this.study = study;
}
}
When I call from client: "url/getsamplesxanalysis" It executes :
当我从客户端调用:“url/getsamplesxanalysis”时,它执行:
@CrossOrigin
@RequestMapping("/getsamplesxanalysis")
public Integer getSamplesXAnalysis() {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
SampleDAO dao = context.getBean(SampleDAO.class);
return dao.getSamplesXAnalysis((long) 1);
}
And no error or exception is given, I receive the result. But If I try to use a parameter on the URL, for example: "url/getsamplesxanalysis/2" It executes :
没有错误或例外,我收到结果。但是如果我尝试在URL上使用一个参数,例如:“URL /getsamplesxanalysis/2”,它会执行:
@CrossOrigin
@RequestMapping("/getsamplesxanalysis/{id}")
public Integer getSamplesXAnalysis(@PathParam("id") Long id) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
SampleDAO dao = context.getBean(SampleDAO.class);
return dao.getSamplesXAnalysis(id);
}
and I get an exception : org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts
我有一个例外:org. postgrel .util。错误:操作符不存在:bigint = bytea提示:没有操作符匹配给定的名称和参数类型。您可能需要添加显式类型转换
So there must be a problem converting argument typem. I looked for but I did not found any solution when the query is anotated.
所以转换参数类型肯定有问题。我寻找了,但是当查询被打乱时,我没有找到任何解决方案。
EDITED After changing return dao.getSamplesXAnalysis(id); to return dao.getSamplesXAnalysis(id.longValue());
修改return dao.getSamplesXAnalysis(id)后进行编辑;返回dao.getSamplesXAnalysis(id.longValue());
NullPointerException trace:
NullPointerException跟踪:
Hibernate:
select
count(sample0_.sample_id) as col_0_0_
from
sample sample0_
where
dna_study_id=?
2016-02-24 12:33:06.917 INFO 1956 --- [nio-8080-exec-3] o.s.c.s.ClassPathXmlApplicationContext : Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@277d9: startup date [Wed Feb 24 12:33:06 CET 2016]; root of context hierarchy
2016-02-24 12:33:06.918 INFO 1956 --- [nio-8080-exec-3] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [applicationContext.xml]
2016-02-24 12:33:07.029 INFO 1956 --- [nio-8080-exec-3] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-02-24 12:33:07.032 INFO 1956 --- [nio-8080-exec-3] o.s.j.d.DriverManagerDataSource : Loaded JDBC driver: org.postgresql.Driver
2016-02-24 12:33:07.035 INFO 1956 --- [nio-8080-exec-3] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'jpaData'
2016-02-24 12:33:07.035 INFO 1956 --- [nio-8080-exec-3] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: jpaData
...]
2016-02-24 12:33:07.270 INFO 1956 --- [nio-8080-exec-3] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2016-02-24 12:33:07.273 INFO 1956 --- [nio-8080-exec-3] o.h.e.jdbc.internal.LobCreatorBuilder : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2016-02-24 12:33:07.296 INFO 1956 --- [nio-8080-exec-3] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2016-02-24 12:33:07.346 INFO 1956 --- [nio-8080-exec-3] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2016-02-24 12:33:07.347 INFO 1956 --- [nio-8080-exec-3] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000102: Fetching database metadata
2016-02-24 12:33:07.374 INFO 1956 --- [nio-8080-exec-3] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000396: Updating schema
2016-02-24 12:33:07.414 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000261: Table found: public.datasource
2016-02-24 12:33:07.414 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000037: Columns: [link, name, data_source_id]
2016-02-24 12:33:07.414 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000108: Foreign keys: []
2016-02-24 12:33:07.415 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000126: Indexes: [datasource_pkey]
2016-02-24 12:33:07.446 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000261: Table found: public.dna_study
2016-02-24 12:33:07.446 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000037: Columns: [date, dna_study_id, name, description]
2016-02-24 12:33:07.446 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000108: Foreign keys: []
2016-02-24 12:33:07.446 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000126: Indexes: [dna_study_id]
2016-02-24 12:33:07.478 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000261: Table found: public.sample
2016-02-24 12:33:07.478 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000037: Columns: [dna_study_id, sample_id, description, id]
2016-02-24 12:33:07.478 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000108: Foreign keys: [fk_hhiykwraml4x7uwou4l1o56df]
2016-02-24 12:33:07.478 INFO 1956 --- [nio-8080-exec-3] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000126: Indexes: [sample_pkey]
2016-02-24 12:33:07.478 INFO 1956 --- [nio-8080-exec-3] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000232: Schema update complete
2016-02-24 12:33:07.482 WARN 1956 --- [nio-8080-exec-3] o.h.j.i.EntityManagerFactoryRegistry : HHH000436: Entity manager factory name (jpaData) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
2016-02-24 12:33:07.537 ERROR 1956 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.gembiosoft.gemdomus.rest.SampleController.getSamplesXAnalysis(SampleController.java:44) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_74]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_74]
EDITED 2 changing parameter method from Long to long :
编辑了2个参数由长变长的方法:
public Integer getSamplesXAnalysis(@PathParam("id") long id) { ... }
returns IlelgalStateException:
返回IlelgalStateException:
java.lang.IllegalStateException: Optional long parameter 'id' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.handleNullValue(AbstractNamedValueMethodArgumentResolver.java:205) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:98) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_74]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_74]
1 个解决方案
#1
0
Here as an answer:
在这里作为一个答案:
The NPE occurs in your SampleController:
NPE发生在您的SampleController:
SampleController.getSamplesXAnalysis(SampleController.java:44)
Which line is 44? I would say it's
44是哪条线?我想说的
return dao.getSamplesXAnalysis(id.longValue());
so there could be two things that can be null: dao or id. I guess it is the id not the dao. I believe your calling you controller with an id, but this id does not make it to your method.
有两种东西可以是空的:dao或id,我猜是id而不是dao。我相信你用id调用你的控制器,但是这个id不能到达你的方法。
Use @PathVariable instead of @PathParam and everything should be fine.
使用@PathVariable而不是@PathParam,一切应该没问题。
EDIT
编辑
Reason it is null is because @PathParam works when a query parameter is defined on the URL, for example: url/getsamplesxanalysis/?id=2
. Since I'm using url/getsamplesxanalysis/2
, @PathVariable is required.
之所以为null,是因为在URL上定义查询参数时,@PathParam可以工作,例如:URL /getsamplesxanalysis/?id=2。因为我使用的是url/getsamplesxanalysis/2,所以需要@PathVariable。
- @PathVariable is to obtain some placeholder from the URI (Springs call it an URI Template)
- @PathVariable是从URI中获取占位符(spring将其称为URI模板)
- @RequestParam is to obtain a parameter
- @RequestParam获取一个参数
Source: @RequestParam vs @PathVariable
来源:@RequestParam vs @PathVariable
#1
0
Here as an answer:
在这里作为一个答案:
The NPE occurs in your SampleController:
NPE发生在您的SampleController:
SampleController.getSamplesXAnalysis(SampleController.java:44)
Which line is 44? I would say it's
44是哪条线?我想说的
return dao.getSamplesXAnalysis(id.longValue());
so there could be two things that can be null: dao or id. I guess it is the id not the dao. I believe your calling you controller with an id, but this id does not make it to your method.
有两种东西可以是空的:dao或id,我猜是id而不是dao。我相信你用id调用你的控制器,但是这个id不能到达你的方法。
Use @PathVariable instead of @PathParam and everything should be fine.
使用@PathVariable而不是@PathParam,一切应该没问题。
EDIT
编辑
Reason it is null is because @PathParam works when a query parameter is defined on the URL, for example: url/getsamplesxanalysis/?id=2
. Since I'm using url/getsamplesxanalysis/2
, @PathVariable is required.
之所以为null,是因为在URL上定义查询参数时,@PathParam可以工作,例如:URL /getsamplesxanalysis/?id=2。因为我使用的是url/getsamplesxanalysis/2,所以需要@PathVariable。
- @PathVariable is to obtain some placeholder from the URI (Springs call it an URI Template)
- @PathVariable是从URI中获取占位符(spring将其称为URI模板)
- @RequestParam is to obtain a parameter
- @RequestParam获取一个参数
Source: @RequestParam vs @PathVariable
来源:@RequestParam vs @PathVariable