0

Spring boot sample program with Mysql backend. very simple code. I checked application. properties, access to the DB etc. No clue why I am getting this error.

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.2.0.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
    </parent>
 <groupId>com.skb.course.apis</groupId>
<artifactId>library-apis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>library-apis</name>
<description>Project for developing Library APIs</description>

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

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- JPA Data (We are going to use Repositories, Entiries, Hibernate, etc...) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- For MySQL Connector-J -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- For implementing API Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- For handling JWT related functionality-->
    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.8.0</version>
    </dependency>

    <!-- Test related dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Used for Integration Tests. Spring's TestRestTemplate throws an error while sending PUT requests with
     authorization error: java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
     Therefore we need to use Apaches's HTTP client. Please refer:
     https://stackoverflow.com/questions/16748969/java-net-httpretryexception-cannot-retry-due-to-server-authentication-in-stream
        -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
</dependencies>

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

enter image description here

Console has no errors and tomcat is configured to run on 3000 port

enter image description here

application.properties file

enter image description here

Mysql db showing list of databaseds

enter image description here

Theere is data available in the PUBLISHER TABLE. but access denied error is thrown

enter image description here

Rest controller code

package com.skb.course.apis.libraryapis.publisher;

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;

 import javax.validation.Valid;
 import java.util.UUID;

 @RestController
 @RequestMapping(path = "/v1/publishers")
 public class PublisherController {

 private static Logger logger = 
 LoggerFactory.getLogger(PublisherController.class);

 private PublisherService publisherService;

   public PublisherController(PublisherService publisherService) {
    this.publisherService = publisherService;
   }

   @GetMapping(path = "/{publisherId}")
   public ResponseEntity<?> getPublisher(@PathVariable Integer publisherId,
        @RequestHeader(value = "Trace-Id", defaultValue = "") String traceId)
        throws LibraryResourceNotFoundException {

    if (!LibraryApiUtils.doesStringValueExist(traceId)) {
        traceId = UUID.randomUUID().toString();
    }

    return new ResponseEntity<>(publisherService.getPublisher(publisherId, traceId), HttpStatus.OK);
}

}

Jason
  • 497
  • 3
  • 9
  • 27
  • Can you share rest controller with mapping /v1/publishers/{number} – gungor Sep 29 '20 at 16:11
  • If you are using maven could you share you pom.xml? Please check whether you have right spring security configurations if you have included any security related starters – balias Sep 29 '20 at 18:04
  • gungor, I added the controller code in my orginal post – Jason Sep 29 '20 at 18:20
  • Remove Spring Security dependency (spring-boot-starter-security) from your pom.xml and then try again! – Onur Baştürk Sep 29 '20 at 18:59
  • Onur, thank for responding. when I remove the spring-boot-starter-security, it throws so many error in the project structure. this is the sample code from a Udemy course. You suspect something with that dependency? – Jason Sep 29 '20 at 19:32

0 Answers0