GET request is not answered correctly by backend

Asked

Viewed 239 times

5

The project is an Angular 6 front-end, which connects to a Java back-end for communication with a SQL Server BD.

The server that receives the file .war, containing both the back and the front, is Tomcat 8.5.32.

The login execution structure follows four files:

  1. The Authcontroller class, which makes the login itself official.

    @RestController
    @RequestMapping(value = "/api")
    public class AuthController {
    
    public static final Logger logger = LoggerFactory.getLogger(AuthController.class);
    
    @CrossOrigin
    @RequestMapping("/auth")
    public Principal user(Principal principal) {
        logger.info("usuário logado " + principal);
        return principal;
      }
    }
    
  2. The Loginrepository interface, to make queries in BD

    @Transactional
    public interface LoginRepository  extends JpaRepository<Login, Long>{
        Login findOneByUsername(String username);
    }
    
  3. The class-service Userservice, which performs queries in the BD

    @Service
    public class UserService {
      @Autowired
      LoginRepository userRepository;
    
      public Login save(Login user) {
         return userRepository.saveAndFlush(user);
      }
    
      public Login update(Login user) {
         return userRepository.save(user);
      }
    
      public Login find(String userName) {
         return userRepository.findOneByUsername(userName);
      }
    
      public Login find(Long id) {
         return userRepository.findOne(id);
      }
    
    }
    
  4. And a second class-service Userdetailsserviceimpl

    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Autowired
    UserService userService;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Login user = userService.find(username);
        return user;
      }
    
    }
    

When sending a GET request to endpoint /api/auth in development environment, with the server running in spring-boot, the login goes smoothly, receiving the Main object and then logging into the system.

When I step into the format war with the appropriate adaptations, tests I did show that the requests to the other tables follow without problem, with the exception of Login, which accesses the database and searches for the credential, but does not return the Main object, but an error 404.

EDIT

Snippets of pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.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>
    <start-class>br.com.tradeturismo.ServletInitialzer</start-class>
    <maven.build.timestamp.format>ddMMyyyyHHmmss</maven.build.timestamp.format>
    <angular.project.location>portal</angular.project.location>
    <angular.project.nodeinstallation>node_installation</angular.project.nodeinstallation>
</properties>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <workingDirectory>${angular.project.location}</workingDirectory>
                <installDirectory>${angular.project.nodeinstallation}</installDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v10.6.0</nodeVersion>
                        <npmVersion>6.1.0</npmVersion>
                    </configuration>
                </execution>

                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>

                <execution>
                    <id>npm build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>default-copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>false</overwrite>
                        <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/${angular.project.location}/dist</directory>
                                <includes>
                                    <include>**/*.*</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Hello! What returns then? An error?

  • If I’m not mistaken, he returns either a 404 or a 401

  • @Dherik edited the question with the back-end answer

  • As you gave a "Not Found Exception", you have the Tomcat log snippet to make it easier to identify what is not being found?

  • @ayowoleagbedejobi was not a java Exception, but an HTTP 404 error

  • you read in this tutorial, I think your problem is in port conflict that you are using and how to declare: https://www.devglan.com/spring-boot/spring-boot-angular-example

  • 1

    @A programmer earlier today in my research I ended up finding just this tutorial. I’m trying to adapt it to my case at this time hehe

  • @mutlei, which adaptations were made to make the WAR deploy?

  • Changes in pom.xml. indicating the initial class of the project, plus settings to do the integration. I put the plugins of the answer to this question.

  • I added some details of pom.xml to the question

Show 5 more comments

2 answers

1


I finally figured out the mystery.

I had to change the annotation value @RequestMapping for <nome-do-projeto>-<versão>.

For example, if your project calls "Eneias" and its version is "1.0.0", the annotation should look like this:

@RequestMapping(value = "/eneias-1.0.0")

This is only for the first annotation, which is next to the class declaration. The second is unchanged.

-1

Are you considering the context of the application to call? by default is the name of your file [name]. War

It would have to be something like this: http://localhost:8080/[name]/api/auth

  • Yes, I am. The error remains the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.