解决方法:“出现意外错误(类型=未找到,状态= 404)”

时间:2019-11-13 20:18:10

标签: java spring-boot

我正在使用此控制器启动Spring Boot应用程序

@RestController
@RequestMapping({"/api"})
public class ProduitImmobilierController {

    Logger logger = LoggerFactory.getLogger(ProduitImmobilierController.class);

    @Autowired
    private ProduitImmobilierService produitImmobilierService;

    @RequestMapping(value = "/produitimmobilier/all/{pageSize}/{page}",
    method = RequestMethod.GET,
    produces = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE},
    consumes = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody List<ProduitImmobilierDTO> findAll(@PathVariable("pageSize") int pageSize, @PathVariable("page") int page){
        logger.info("CONTROLLER PRODUITIMMOBILIERSERVICE CA PASSE");
        return produitImmobilierService.findAll(pageSize, page);
    }

我检查应用程序是否正在侦听端口8080 我尝试在firefox上使用此URL访问此控制器

http://localhost:8080/api/produitimmobilier/all/5/1

我收到此错误

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Nov 13 20:48:53 CET 2019
There was an unexpected error (type=Not Found, status=404).
No message available

这是项目树

enter image description here

我该如何解决?

我添加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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demoImmobilierBack</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoImmobilierBack</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.hql</groupId>
            <artifactId>hibernate-hql-parser</artifactId>
            <version>1.5.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>   
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
<!--            <version>1.4.194</version> -->
        </dependency>   
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>       
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

当我使用以下网址访问应用程序时:http://localhost:8080/api/produitimmobilier/all/5/1

2019-11-14 12:20:58.770  INFO 2998 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-14 12:20:58.770  INFO 2998 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-11-14 12:20:58.787  INFO 2998 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms

4 个答案:

答案 0 :(得分:0)

尝试:

@RestController
@RequestMapping("/api")
    public class ProduitImmobilierController {
        ...
        ...
        ... 
    }

答案 1 :(得分:0)

我更改了包含main方法的类,如下所示添加了@ComponentScan

class PrimaryReplicaRouter:
    def db_for_read(self, model, **hints):
        """
        Reads go to a read replica. We can add 'read_replica', 'write_replica'
        """
        return 'primary'

    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        return 'primary'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        """
        return True

class Auth:
     def db_for_read(self, model, **hints):
        return 'auth'

    def db_for_write(self, model, **hints):
        return 'auth'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        return True

当我点击网址时:

{
    "errorCode": "3703",
    "message": "Invalid delete activity payload with 'folderPath' that is required and cannot be empty.",
    "failureType": "UserError",
    "target": "DeleteCSVFromFTPServer"
}

我收到以下错误

package com.example.demoImmobilierBack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"com.example.demoImmobilierBack.service","com.example.demoImmobilierBack", "com.example.demoImmobilierBack.controller", "com.example.demoImmobilierBack.repository"})
public class DemoImmobilierBackApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoImmobilierBackApplication.class, args);
    }
}

我阅读了有关错误406的信息

http://localhost:8080/api/produitimmobilier/all/5/1

我在firefox上工作,并尝试使用User-Agent Switcher更改用户代理,以便获得正确的响应,即json响应,而不会成功

我还有另一个问题。当我尝试在应用程序中添加具有其他服务的另一个控制器时,即

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 14 21:11:26 CET 2019
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

启动应用程序时,出现以下错误,好像无法扫描UserService

The 406 Not Acceptable is an HTTP response status code indicating that the client has requested a response using Accept- headers that the server is unable to fulfill. This is typically a result of the user agent (i.e. browser) specifying an acceptable character set (via Accept-Charset), language (via Accept-Language), and so forth that should be responded with, and the server being unable to provide such a response.

这是树的层次结构

enter image description here

答案 2 :(得分:0)

是的,实际上,当我输入以下内容时,它会起作用

@SpringBootApplication(scanBasePackages =“ com.example.demoImmobilierBack”)

但是我仍然遇到UserService问题。这是界面

package com.example.demoImmobilierBack.service;

import com.example.demoImmobilierBack.dto.UserDTO;
import com.example.demoImmobilierBack.model.User;

public interface UserService {

    UserDTO findByEmailAndPassword(String email, String password);

    UserDTO save(UserDTO userDTO);

    String checkIfUserExistsAndGoodCredential(UserDTO userDTO);

    UserDTO convertUserToUserDTO(User user);

    User convertUserDTOToUser(UserDTO userDTO);

    String checkEmailAndPasswordAndPasswordConfirm(UserDTO userDTO);
}

这是实现

package com.example.demoImmobilierBack.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;

import com.example.demoImmobilierBack.dto.ProduitImmobilierDTO;
import com.example.demoImmobilierBack.dto.UserDTO;
import com.example.demoImmobilierBack.model.ProduitImmobilier;
import com.example.demoImmobilierBack.model.User;
import com.example.demoImmobilierBack.repository.ProduitImmobilierRepository;
import com.example.demoImmobilierBack.repository.UserRepository;

public class UserServiceImpl implements UserService{

    public static final Pattern VALID_EMAIL_ADDRESS_REGEX = 
            Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

    public static final Pattern VALID_PASSWORD_REGEX = 
            Pattern.compile("(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&].{8,}");

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ProduitImmobilierService produitImmobilierService;

    public UserDTO findByEmailAndPassword(String email, String password) {
        User user = userRepository.findByEmailAndPassword(email, password);
        return convertUserToUserDTO(user);
    }

    public UserDTO save(UserDTO userDTO) {
        User user = userRepository.save(convertUserDTOToUser(userDTO));
        return convertUserToUserDTO(user);
    }

    public String checkEmailAndPasswordAndPasswordConfirm(UserDTO userDTO) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(userDTO.getEmail());
        if (!matcher.find()) {
            return "L'email que vous avez fourni: " + userDTO.getEmail() + " n'est pas valide";
        }
        User user = userRepository.findByEmail(userDTO.getEmail());
        if (user != null) {
            return "L'utilisateur avec l'email suivant " + userDTO.getEmail() + " existe déjà";
        }
        matcher = VALID_PASSWORD_REGEX .matcher(userDTO.getPassword());
        if (!matcher.find()) {
            return "Le mot de passe que vous avez fourni: " + userDTO.getPassword() + " n'est pas valide. Il doit contenir au moins 8 caractères, un caractère minuscule, un caractère majuscule, un chiffre et un caractère spécial.";
        }
        if (!userDTO.getPassword().equals(userDTO.getPasswordConfirm())) {
            return "Le mot de passe que vous avez fourni: " + userDTO.getPassword() + " ne correspond pas à la confirmation du mot de passe  " + userDTO.getPasswordConfirm();
        }
        return "";
    }

    public String checkIfUserExistsAndGoodCredential(UserDTO userDTO) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(userDTO.getEmail());
        if (!matcher.find()) {
            return "L'email que vous avez fourni: " + userDTO.getEmail() + " n'est pas valide";
        }
        User user = userRepository.findByEmail(userDTO.getEmail());
        if (user == null) {
            return "L'utilisateur avec l'email suivant " + userDTO.getEmail() + " n'existe pas";
        }
        matcher = VALID_PASSWORD_REGEX .matcher(userDTO.getPassword());
        if (!matcher.find()) {
            return "Le mot de passe que vous avez fourni: " + userDTO.getPassword() + " n'est pas valide. Il doit contenir au moins 8 caractères, un caractère minuscule, un caractère majuscule, un chiffre et un caractère spécial.";
        }
        user = userRepository.findByEmailAndPassword(userDTO.getEmail(), userDTO.getPassword());
        if (user == null) {
            return "L'utilisateur avec l'email suivant " + userDTO.getEmail() + " et le mot de passe suivant " + userDTO.getPassword() + "n'existe pas";
        }   
        return "";
    }

    public UserDTO convertUserToUserDTO(User user) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setGender(user.getGender());
        userDTO.setLastName(user.getLastName());
        userDTO.setEmail(user.getEmail());
        userDTO.setPassword(user.getPassword());
        userDTO.setMobileTelephone(user.getFixedTelephone());
        userDTO.setFixedTelephone(user.getFixedTelephone());
        userDTO.setAdress(user.getAdress());
        userDTO.setPostalCode(user.getPostalCode());
        userDTO.setTown(user.getTown());
        userDTO.setProfession(user.getProfession());
        userDTO.setProfile(user.getProfile());
        userDTO.setMaritalSituation(user.getMaritalSituation());
        userDTO.setChildrenNumber(user.getChildrenNumber());
        userDTO.setDependant(user.getDependant());
        userDTO.setOwnedOrRentedAccommodation(user.getOwnedOrRentedAccommodation());
        userDTO.setMonthlyNetSalary(user.getMonthlyNetSalary());
        userDTO.setRentAmount(user.getRentAmount());
        userDTO.setIsBankLoan(user.getIsBankLoan());
        userDTO.setCapitalContribution(user.getCapitalContribution());
        userDTO.setCreditAmount(user.getCreditAmount());
        userDTO.setMonthlyPayment(user.getMonthlyPayment());
        userDTO.setCreditTerminationDate(user.getCreditTerminationDate());
        userDTO.setSIRETNumber(user.getSIRETNumber());

        Set<ProduitImmobilier> setProduitImmobiliers = user.getRealEstateProperty();
        List<ProduitImmobilierDTO> setProduitImmobilierDTOs = new ArrayList<>();

        setProduitImmobiliers.forEach(p ->{ ProduitImmobilierDTO pDTO = produitImmobilierService.convertProduitImmobilierToProduitImmobilierDTO(p, setProduitImmobiliers.size()); setProduitImmobilierDTOs.add(pDTO);});

        Comparator<ProduitImmobilierDTO> compareByIdDesc = new Comparator<ProduitImmobilierDTO>() {
            @Override
            public int compare(ProduitImmobilierDTO o1, ProduitImmobilierDTO o2) {
                return o2.getId().compareTo(o1.getId());
            }
        };
        Collections.sort(setProduitImmobilierDTOs, compareByIdDesc);
        userDTO.setRealEstateProperty(setProduitImmobilierDTOs);
        return userDTO;
    }

    public User convertUserDTOToUser(UserDTO userDTO) {
        User user = new User();
        user.setId(userDTO.getId());
        user.setGender(userDTO.getGender());
        user.setLastName(userDTO.getLastName());
        user.setEmail(userDTO.getEmail());
        user.setPassword(userDTO.getPassword());
        user.setMobileTelephone(userDTO.getFixedTelephone());
        user.setFixedTelephone(userDTO.getFixedTelephone());
        user.setAdress(userDTO.getAdress());
        user.setPostalCode(userDTO.getPostalCode());
        user.setTown(userDTO.getTown());
        user.setProfession(userDTO.getProfession());
        user.setProfile(userDTO.getProfile());
        user.setMaritalSituation(userDTO.getMaritalSituation());
        user.setChildrenNumber(userDTO.getChildrenNumber());
        user.setDependant(userDTO.getDependant());
        user.setOwnedOrRentedAccommodation(userDTO.getOwnedOrRentedAccommodation());
        user.setMonthlyNetSalary(userDTO.getMonthlyNetSalary());
        user.setRentAmount(userDTO.getRentAmount());
        user.setIsBankLoan((userDTO.getIsBankLoan().equals(new Byte((byte) 0))) ? false : userDTO.getIsBankLoan().equals(new Byte((byte) 1)) ? true : null  );
        user.setCapitalContribution(userDTO.getCapitalContribution());
        user.setCreditAmount(userDTO.getCreditAmount());
        user.setMonthlyPayment(userDTO.getMonthlyPayment());
        user.setCreditTerminationDate(new Date(userDTO.getCreditTerminationDate()));
        user.setSIRETNumber(userDTO.getSIRETNumber());
        return user;
    }

}

答案 3 :(得分:0)

我终于找到了解决方案。我只是忘了为UserServiceImpl添加@Service注释。谢谢您的回复