我是spring和Hibernate的新手。我想使用这些技术开发WS应用程序。
出现问题,我创建了控制器,服务,dao,并且还在AppConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScans(value = {@ComponentScan("com.example.ws.maven_web_service_project.service"),
@ComponentScan("com.example.ws.maven_web_service_project.dao")})
public class AppConfig {
@Bean
public LocalSessionFactoryBean sessionFactoryBean(){
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(new String[] { "com.example.ws.maven_web_service_project.model" });
sessionFactoryBean.setHibernateProperties(hibernateProperties());
return sessionFactoryBean;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/hibernate_test");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
private Properties hibernateProperties(){
Properties props = new Properties();
props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
props.put("hibernate.show_sql", "true");
props.put("hibernate.hbm2ddl.auto", "update");
// Setting C3P0 properties
props.put("hibernate.c3p0.min_size", 5);
props.put("hibernate.c3p0.max_size", 20);
props.put("hibernate.c3p0.acquire_increment",
1);
props.put("hibernate.c3p0.timeout", 1800);
props.put("hibernate.c3p0.max_statements", 150);
return props;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
在BookController.java
中,定义了请求映射
@RestController
public class BookController {
@Autowired
private BookService bookService;
@PostMapping("/book")
public ResponseEntity<?> saveBook(@RequestBody Book book){
int bookId = bookService.saveBook(book);
return ResponseEntity.ok().body("Your Book with Id "+ bookId +" save successfully.");
}
@GetMapping("/book/{id}")
public ResponseEntity<Book> getBook(@PathVariable("id") int bookId){
Book book = bookService.getBook(bookId);
return ResponseEntity.ok().body(book);
}
@GetMapping("/book")
public ResponseEntity<List<Book>> list() {
List<Book> books = bookService.listBooks();
return ResponseEntity.ok().body(books);
}
}
我启动了tomcat 9
并且它已经启动但是当我尝试触发URL https://localhost:8080/RakeshMavenProject/book/
时,控制台中会打印以下异常
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:410)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:291)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
我已经搜索了很多内容,但无法在任何地方找到解决方案。
请有人帮我找到解决方案。