【发布时间】:2017-04-12 17:31:19
【问题描述】:
我正在尝试运行一个 Spring Boot 项目。当我选择 1.5.2 版时,requestMapping url 不起作用。它总是给我404错误。
如果缺少任何标签或版本中的任何设置更改,有人可以确认我。或任何其他我错过的东西。
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.prizy</groupId>
<artifactId>ProductApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ProductApi</name>
<description>Project for Product Managment</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
控制器:
package com.prizy.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.prizy.model.Product;
import com.prizy.service.ProductService;
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping("/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
@RequestMapping("/hi")
public String getString() {
return "Hi";
}
@RequestMapping("/products/{productId}")
public Product getProduct(@PathVariable int productId){
return productService.getTopic(productId);
}
@RequestMapping(method=RequestMethod.POST, value="/products")
public void addProduct(@RequestBody Product product) {
productService.addProduct(product);
}
@RequestMapping(method=RequestMethod.PUT, value="/products/{productId}")
public void updateProduct(@RequestBody Product product, @PathVariable int productId) {
productService.updateProduct(product,productId);
}
@RequestMapping(method=RequestMethod.DELETE, value="/products/{productId}")
public void deleteProduct(@PathVariable int productId) {
productService.deleteProduct(productId);
}
}
主类是
package com.prizy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAutoConfiguration
@SpringBootApplication
public class ProductApiApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApiApplication.class, args);
}
}
注意:问题已更新,包含更多信息。
【问题讨论】:
-
您要访问的网址是什么?
-
package com.prizy.controller: 你的主班在哪里? -
你能把你的主类和包名一起发布吗?
-
这个类的其他方法有用吗?不应该使用
get...方法名称吗?您是否尝试过更改方法的名称或将/hi定位到另一个方法?
标签: java spring maven spring-mvc spring-boot