【问题标题】:Spring MVC controller ignoredSpring MVC 控制器被忽略
【发布时间】:2021-08-26 17:49:10
【问题描述】:

我已经开始玩 Springboot 和 Spring MVC

我没有遇到任何问题,但现在我正在尝试利用安全性。

使用以下 pom、控制器和 curl 请求,curl 请求将被完全忽略。

如果我使用调试运行,在控制器中有一个停止点,它永远不会到达那里。 没有错误消息,但有数百行否定匹配。

我的控制器没有显示在映射下。

我错过了什么?

pom

<?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.5.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.xxx</groupId>
    <artifactId>address-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>address-services</name>
    <description>REST services for address validation</description>
    
    <properties>
        <java.version>11</java.version>
            <skip_unit_tests>false</skip_unit_tests>
        <org.slf4j.version>1.7.28</org.slf4j.version>
        <org.xxx.version>9.2.0</org.xxx.version>
        <com.h2database.version>1.4.199</com.h2database.version>  
        <commons.lang.version>2.4</commons.lang.version>
        <commons.io.version>2.6</commons.io.version>
        <dom4j.version>1.6.1</dom4j.version>
        <!--  <org.springframework.version>5.2.7.RELEASE</org.springframework.version> -->
    </properties>
    
    <dependencies>
        <!--  spring  -->
        <!-- 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- xxx -->
        <dependency>
            <groupId>org.xxx</groupId>
            <artifactId>xxx-core</artifactId>
            <version>${org.xxx.version}</version>
    
                <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.xxx</groupId>
            <artifactId>xxx-test</artifactId>
            <version>${org.xxx.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- <dependency> <groupId>org.xxx</groupId> <artifactId>xxx-hibernate</artifactId> 
            <version>${org.xxx.hibernate.version}</version> </dependency> -->
        <dependency>
            <groupId>org.xxx</groupId>
            <artifactId>xxx-jcmdline</artifactId>
            <version>${org.xxx.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.xxx</groupId>
            <artifactId>xxx-jcmdline</artifactId>
            <version>${org.xxx.version}</version>
        </dependency>

        <!--  others  -->
        <!-- 
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
            <scope>test</scope>
        </dependency>
        -->
    </dependencies>

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

</project>

控制器

包org.xxx.address.service;

import java.util.ArrayList;
import java.util.List;

import org.xxx.address.beans.AddressBean;
import org.xxx.address.beans.AddressList;
import org.xxx.address.service.usps.UspsAddressValidator;
import org.xxx.address.usps.AddressValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Controller
public class ValidationRest {

    Logger logger = LoggerFactory.getLogger(getClass());
    
    public ValidationRest() {
    }
    
    @PostMapping(value="/validateAddress")
    public String validateAddress(
            @ModelAttribute String address1,
            @ModelAttribute String address2,
            @ModelAttribute String     city,
            @ModelAttribute String     state,
            @ModelAttribute String       zip,
            Model model) throws AddressValidationException {
        AddressBean address = new AddressBean(address1,address2,city,state,zip);
        List<AddressBean> list = new ArrayList<AddressBean>();
        list.add(address);
        UspsAddressValidator validator = new UspsAddressValidator();
         List<AddressBean> response = null;
        try {
            response = validator.validate(list);
        } catch (AddressValidationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }
        model.addAttribute("response",response);
        return "validatedAddress";
    }

卷曲

echo rooter > curl -u root -X POST
localhost:8080/validateAddress/&address1=410SouthMain&address2=&city=Romeo&state=MI&zip=78723

标准输出/标准错误

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

INFO  - 11:55:02,777 AddressApplication - Starting AddressApplication v0.0.1-SNAPSHOT using Java 14.0.2 on atx with PID 2737903 (/home/jjs/git/diamond-21-windoze/javautil/address-services/target/address-services-0.0.1-SNAPSHOT.jar started by jjs in /home/jjs/git/diamond-21-windoze/javautil/address-services)
INFO  - 11:55:02,777 AddressApplication - No active profile set, falling back to default profiles: default
Jun 09, 2021 11:55:03 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jun 09, 2021 11:55:03 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.46]
Jun 09, 2021 11:55:03 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
INFO  - 11:55:04,220 AddressApplication - Started AddressApplication in 1.74 seconds (JVM running for 2.089)

Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   continues for hundreds of lines.

映射

DEBUG Mappings - 
    o.s.d.r.w.RepositoryController:
    {OPTIONS [/ || ], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: optionsForRepositories()
    {HEAD [/ || ], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: headForRepositories()
    {GET [/ || ], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: listRepositories()
DEBUG Mappings - 
    o.s.d.r.w.RepositoryEntityController:
    {POST [/{repository}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: postCollectionResource(RootResourceInformation,PersistentEntityResource,PersistentEntityResourceAssembler,String)
    {OPTIONS [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: optionsForItemResource(RootResourceInformation)
    {HEAD [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: headForItemResource(RootResourceInformation,Serializable,PersistentEntityResourceAssembler)
    {GET [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: getItemResource(RootResourceInformation,Serializable,PersistentEntityResourceAssembler,HttpHeaders)
    {PUT [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: putItemResource(RootResourceInformation,PersistentEntityResource,Serializable,PersistentEntityResourceAssembler,ETag,String)
    {PATCH [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: patchItemResource(RootResourceInformation,PersistentEntityResource,Serializable,PersistentEntityResourceAssembler,ETag,String)
    {DELETE [/{repository}/{id}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: deleteItemResource(RootResourceInformation,Serializable,ETag)
    {OPTIONS [/{repository}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: optionsForCollectionResource(RootResourceInformation)
    {HEAD [/{repository}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: headCollectionResource(RootResourceInformation,DefaultedPageable)
    {GET [/{repository}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: getCollectionResource(RootResourceInformation,DefaultedPageable,Sort,PersistentEntityResourceAssembler)
    {GET [/{repository}], produces [application/x-spring-data-compact+json || text/uri-list]}: getCollectionResourceCompact(RootResourceInformation,DefaultedPageable,Sort,PersistentEntityResourceAssembler)
DEBUG Mappings - 
    o.s.d.r.w.RepositoryPropertyReferenceController:
    {GET [/{repository}/{id}/{property}/{propertyId}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: followPropertyReference(RootResourceInformation,Serializable,String,String,PersistentEntityResourceAssembler)
    {GET [/{repository}/{id}/{property}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: followPropertyReference(RootResourceInformation,Serializable,String,PersistentEntityResourceAssembler)
    {DELETE [/{repository}/{id}/{property}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: deletePropertyReference(RootResourceInformation,Serializable,String)
    {GET [/{repository}/{id}/{property}], produces [text/uri-list]}: followPropertyReferenceCompact(RootResourceInformation,Serializable,String,HttpHeaders,PersistentEntityResourceAssembler)
    {[PATCH, PUT, POST] [/{repository}/{id}/{property}], consumes [application/json || application/x-spring-data-compact+json || text/uri-list], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: createPropertyReference(RootResourceInformation,HttpMethod,CollectionModel,Serializable,String)
    {DELETE [/{repository}/{id}/{property}/{propertyId}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: deletePropertyReferenceId(RootResourceInformation,Serializable,String,String)
DEBUG Mappings - 
    o.s.d.r.w.RepositorySearchController:
    {OPTIONS [/{repository}/search], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: optionsForSearches(RootResourceInformation)
    {HEAD [/{repository}/search], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: headForSearches(RootResourceInformation)
    {GET [/{repository}/search], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: listSearches(RootResourceInformation)
    {GET [/{repository}/search/{search}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: executeSearch(RootResourceInformation,MultiValueMap,String,DefaultedPageable,Sort,PersistentEntityResourceAssembler,HttpHeaders)
    {GET [/{repository}/search/{search}], produces [application/x-spring-data-compact+json]}: executeSearchCompact(RootResourceInformation,HttpHeaders,MultiValueMap,String,String,DefaultedPageable,Sort,PersistentEntityResourceAssembler)
    {OPTIONS [/{repository}/search/{search}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: optionsForSearch(RootResourceInformation,String)
    {HEAD [/{repository}/search/{search}], produces [application/hal+json || application/json || application/prs.hal-forms+json]}: headForSearch(RootResourceInformation,String)
DEBUG RepositoryRestHandlerMapping - 27 mappings in org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping
DEBUG Mappings - 
    o.s.d.r.w.RepositorySchemaController:
    {GET [/profile/{repository}], produces [application/schema+json]}: schema(RootResourceInformation)
DEBUG Mappings - 
    o.s.d.r.w.a.AlpsController:
    {GET [/profile/{repository}], produces [application/alps+json || */*]}: descriptor(RootResourceInformation)
    {OPTIONS [/profile/{repository}], produces [application/alps+json]}: alpsOptions()
DEBUG Mappings - 
    o.s.d.r.w.ProfileController:
    {OPTIONS [/profile]}: profileOptions()
    {GET [/profile]}: listAllFormsOfMetadata()

【问题讨论】:

  • 如果您只想调试安全性,您可以在 application.properties 中使用:logging.level.org.springframework.security=DEBUG。请提供使用您的安全配置。瞎猜:你在发帖,开启了csrf保护?

标签: spring-boot spring-mvc spring-security


【解决方案1】:

从映射日志来看,您所拥有的数据依赖项似乎没有从您的项目中清楚地清除。

我认为尝试重新导入项目应该可以解决问题。 另外,你应该使用curl 喜欢:

curl -d "address1=410SouthMain&address2=&city=Romeo&state=MI&zip=78723" -X POST http://localhost:8080/validateAddress

【讨论】:

  • 更改 pom 中的依赖项时,请务必记住 mvn clean compile
猜你喜欢
  • 2014-10-04
  • 2018-03-31
  • 2018-01-31
  • 1970-01-01
  • 2020-11-23
  • 2014-04-24
  • 2015-02-24
  • 1970-01-01
  • 2021-11-04
相关资源
最近更新 更多