enter image description here我在没有基于Java的web.xml配置的情况下运行了我的程序。当我使用基于Java的配置时,邮递员看不到任何查询并返回错误404。
我认为这个问题是由于豆子引起的。放置到处注释。
package com.lesson3.hometask.Controller;
import com.google.gson.Gson;
import com.lesson3.hometask.Model.Storage;
import com.lesson3.hometask.Service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
@Controller
@Component
public class StorageController{
private Service service;
@Autowired
public StorageController(Service service) {
this.service = service;
}
@RequestMapping(method = RequestMethod.POST, value = "/storageSave")
public @ResponseBody
String save(HttpServletRequest req) throws IOException {
return service.save(readValuesPostman(req), Integer.parseInt(req.getParameter("id"))).toString();
}
@RequestMapping(method = RequestMethod.GET, value = "/storageFind")
public @ResponseBody
String findById(HttpServletRequest req) throws IOException {
return service.findById(Storage.class, Integer.parseInt(req.getParameter("id"))).toString();
}
@RequestMapping(method = RequestMethod.DELETE, value = "/storageDelete")
public @ResponseBody
String delete(HttpServletRequest req) throws IOException {
service.delete(Storage.class, Integer.parseInt(req.getParameter("id")));
return service.findById(Storage.class, Integer.parseInt(req.getParameter("id"))).toString();
}
@RequestMapping(method = RequestMethod.PUT, value = "/storageUpdate")
public @ResponseBody
String update(HttpServletRequest req) throws IOException {
service.update(readValuesPostman(req));
return readValuesPostman(req).toString();
}
private Storage readValuesPostman(HttpServletRequest req) throws IOException {
Storage storage;
try(BufferedReader reader = req.getReader()) {
Gson gson = new Gson();
storage = gson.fromJson(reader, Storage.class);
}
return storage;
}
}
package com.lesson3.hometask.Model;
import javax.persistence.*;
import java.util.Arrays;
@Entity
@Table(name = "STORAGE")
public class Storage {
@Id
@SequenceGenerator(name = "STORAGE_SEQ", sequenceName = "STORAGE_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STORAGE_SEQ")
@Column(name = "ID")
private int id;
@Column(name = "FORMATS_SUPPORTED")
private String formatsSupported;
@Column(name = "STORAGE_COUNTRY")
private String storageCountry;
@Column(name = "STORAGE_MAX_SIZE")
private Long storageMaxSize;
public Storage() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFormatsSupported() {
return formatsSupported;
}
public void setFormatsSupported(String formatsSupported) {
this.formatsSupported = formatsSupported;
}
public String getStorageCountry() {
return storageCountry;
}
public void setStorageCountry(String storageCountry) {
this.storageCountry = storageCountry;
}
public long getStorageMaxSize() {
return storageMaxSize;
}
public void setStorageMaxSize(Long storageMaxSize) {
this.storageMaxSize = storageMaxSize;
}
@Override
public String toString() {
return "Storage{" +
"id=" + id +
", formatsSupported='" + formatsSupported + '\'' +
", storageCountry='" + storageCountry + '\'' +
", storageMaxSize=" + storageMaxSize +
'}';
}
}
package config;
import com.lesson3.hometask.DAO.DAO;
import com.lesson3.hometask.Service.Service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan({"com"})
@EnableWebMvc
public class AppContext implements WebMvcConfigurer {
@Bean(name = "service")
public Service service(){
return new Service();
}
@Bean(name = "DAO")
public DAO dao(){
return new DAO();
}
}
package config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class Initializer implements WebApplicationInitializer {
// Указываем имя нашему Servlet Dispatcher для мапинга
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// Регистрируем в контексте конфигурационный класс, который мы создадим ниже
ctx.register(AppContext.class);
servletContext.addListener(new ContextLoaderListener(ctx));
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
<?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>Spring1.3</groupId>
<artifactId>Spring1.3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
邮递员返回错误404 =“原始服务器找不到目标资源的当前表示形式,或者找不到 愿意透露一个人的存在。”