question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger2 in Spring boot project causes an MethodArgumentTypeMismatchException by NumberFormatException

See original GitHub issue

I use the swagger2 in my srping boot project,

here is my pom.xml

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </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-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.6.1</version>
    </dependency>

and then my restful api :

@ApiOperation(value = "create some producer", notes = "create ....")
@ApiImplicitParam(name = "num", value = "number of producer", required = true, dataType = "Long")
@RequestMapping(value = "/producer/{num}", method = RequestMethod.POST)
public String postMultiNaaSProducer(@PathVariable Long num)
{
...
}

and then I get this error as below when I try the restful api in swagger ui:

{
  "timestamp": 1489027913782,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
  "message": "Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: \"{num}\"",
  "path": "/rocketmq/producer/%7Bnum%7D"
}

I don’t know why will it happened. And the api works well when I invoke by postman…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Jack2codecommented, Mar 9, 2017

I’m sorry, I miss the paramType="path" in @ApiImplicitParam annotation。

It should be,

 @ApiImplicitParam(name = "num", value = "?????", required = true, dataType = "Long", paramType="path")

and it works well.

without paramType it consider the param num as a body param, so I only get the value of {num}

0reactions
dilipkrishcommented, Mar 9, 2017

Awesome! thanks for reporting back!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Swagger2 with spring boot - java - Stack Overflow
Configuring Swagger2 with spring boot · I think, that problem will be in your path @RequestMapping(path="{/id}", method = RequestMethod.GET) , it ...
Read more >
NumberFormatException: For input string: "" in Swagger ...
In my previous article about Documenting Spring Boot REST API with Swagger, ... NumberFormatException: For input string: "" at java.lang.
Read more >
I can't run any version of swagger-fox. Spring Boot 2.4.2 + JDK 8.
Spring Boot 2.4.2 + JDK 8. I have previously added swagger-fox to my projects using the instruction below, but none fit.
Read more >
Swagger,Knife4j java.lang.NumberFormatException
4. 5. Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException ...
Read more >
Spring Boot + Swagger 2 Simple Hello World example
In this post we configure a spring boot application to integrate swagger2. Spring Boot example we had exposed a REST API . Documentation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found