0

I'm having a serious problem with Spring Boot and Wire Mock. When I run the Application through IntelliJ Idea and hit sample url (http://localhost:8088/mtn-mock-service/hello) then I am able to see response 'Hello!World' and Status code 200. but I run with java command 'java -jar target/mtn-mock-service-0.0.1-SNAPSHOT.jar --spring.profiles.active=local' then I am getting 404 with same url (http://localhost:8088/mtn-mock-service/hello). I have attached code below.

POM.XML

<?xml version="1.0"?>
<project 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"
         xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.poc.wiremock</groupId>
    <artifactId>poc_wiremock</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mock-service</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

    <properties>
        <spring.boot.version>2.7.5</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>11</java.version>
        <maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
        <spring.boot.starter.azure.version>2.4.10</spring.boot.starter.azure.version>
        <maven-checkstyle-plugin.version>3.1.2</maven-checkstyle-plugin.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${maven-checkstyle-plugin.version}</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8</artifactId>
            <version>2.27.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
           <groupId>com.microsoft.azure</groupId>
           <artifactId>azure-spring-boot-starter</artifactId>
            <version>${spring.boot.starter.azure.version}</version>
        </dependency>
    </dependencies>
</project>

WireMockController.java

import com.github.tomakehurst.wiremock.core.WireMockApp;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.jetty9.DefaultMultipartRequestConfigurer;
import com.github.tomakehurst.wiremock.servlet.WireMockHttpServletRequestAdapter;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WiremockController {
    private final WireMockApp wireMockApp;

    @GetMapping(path = "/**")
    @CrossOrigin
    public ResponseEntity<String> getResponse(HttpServletRequest httpServletRequest) {
        return getResponseEntity(httpServletRequest);
    }

    @CrossOrigin
    @PostMapping(path = "/**")
    public ResponseEntity<String> postResponse(HttpServletRequest httpServletRequest) {
        return getResponseEntity(httpServletRequest);
    }

    @CrossOrigin
    @PutMapping(path = "/**")
    public ResponseEntity<String> putResponse(HttpServletRequest httpServletRequest) {
        return getResponseEntity(httpServletRequest);
    }

    @CrossOrigin
    @DeleteMapping(path = "/**")
    public ResponseEntity<String> deleteResponse(HttpServletRequest httpServletRequest) {
        return getResponseEntity(httpServletRequest);
    }

    @CrossOrigin
    @PatchMapping(path = "/**")
    public ResponseEntity<String> patchResponse(HttpServletRequest httpServletRequest) {
        return getResponseEntity(httpServletRequest);
    }

    @GetMapping("/hello_world")
    @CrossOrigin
    public String SampleHello() {
        return "Hello!World";
    }

    private ResponseEntity getResponseEntity(HttpServletRequest httpServletRequest) {
        ServeEvent serveEvent = wireMockApp.serveStubFor(
                new WireMockHttpServletRequestAdapter(httpServletRequest, new DefaultMultipartRequestConfigurer(), StringUtils.EMPTY));
        ResponseDefinition responseDefinition = serveEvent.getResponseDefinition();

        return new ResponseEntity(responseDefinition.getBody(),
                transformHttpHeaders(responseDefinition.getHeaders()),
                HttpStatus.resolve(responseDefinition.getStatus()));
    }

    private HttpHeaders transformHttpHeaders(com.github.tomakehurst.wiremock.http.HttpHeaders wiremockHttpHeaders) {
        HttpHeaders headers = new HttpHeaders();
        if (wiremockHttpHeaders != null)
            wiremockHttpHeaders.all().forEach(httpHeader -> headers.addAll(httpHeader.key(), httpHeader.values()));

        return headers;
    }
}

WiremockConfiguration.java

import com.github.tomakehurst.wiremock.common.ClasspathFileSource;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.core.WireMockApp;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.servlet.NotImplementedContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

@Configuration
public class WiremockConfiguration {

    @Value("${wiremock.stubs-mapping-folder}")
    private String stubsMappingFolder;

    @Bean
    public WireMockApp wireMockApp() {
        return new WireMockApp(options().extensions(new ResponseTemplateTransformer(false))
                .fileSource(new ClasspathFileSourceWithoutLeadingSlash()), new NotImplementedContainer());
    }

    private class ClasspathFileSourceWithoutLeadingSlash extends ClasspathFileSource {

        ClasspathFileSourceWithoutLeadingSlash() {
            super("");
        }

        @Override
        public FileSource child(String subDirectoryName) {
            return new ClasspathFileSource(stubsMappingFolder + subDirectoryName);
        }
    }



}

sample testing.json

{
  "mappings": [
    {
      "request": {
        "method": "POST",
        "url": "/hello"
      },
      "response": {
        "status": 200,
        "body": "POST Hello!"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/hello"
      },
      "response": {
        "status": 200,
        "body": "GET Hello!"
      }
    },
    {
      "request": {
        "method": "PUT",
        "url": "/hello"
      },
      "response": {
        "status": 200,
        "body": "PUT Hello!"
      }
    },
    {
      "request": {
        "method": "PATCH",
        "url": "/hello"
      },
      "response": {
        "status": 200,
        "body": "PATCH Hello!"
      }
    },
    {
      "request": {
        "method": "DELETE",
        "url": "/hello"
      },
      "response": {
        "status": 200,
        "body": "DELETE Hello!"
      }
    }
  ]
}

Run with IntelliJ Screen shots enter image description here

enter image description here

Run with executable jar command screenshots

enter image description here

enter image description here

Packaging screen shot enter image description here Thanks in advance. Waiting reply

0 Answers0