简体   繁体   中英

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'

I'm trying to create a simple spring web app. And i encountered an error while executing POST method. When i go to url to which my post method is mapped i'm getting the following error:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jan 19 20:57:38 EET 2022 There was an unexpected error (type=Bad Request, status=400).

I have an html form where user can enters data, from which an object is created and added to the db (for ui templates i use Mustache)

Also, the error in console is the following:

.wsmsDefaultHandlerExceptionResolver: Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "charity"]

here's my Controller:

@RequestMapping("/share")
@Controller
public class CharityController {

  @Autowired
  private CharityServiceImpl charityService;


  @PostMapping("charities/charity")
  public String addCharity(@RequestBody Charity charity){ 
    charityService.save(charity);
    
    return "addForm";
  } 

html form:

<!DOCTYPE HTML>
<html>
<head>
    <title>Add New Charity</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <div>
        <form method="post" action="charities/charity">
            <input type="number" name="id" placeholder="Id"/>
            <input type="text" name="name" placeholder="Charity name"/>
            <input type="text" name="description" placeholder="Description"/>
            <button type="submit">Add</button>
        </form>
    </div>
</body>
</html>

Entity class:

@Entity
public class Charity {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  @NotBlank
  private String name;
  @NotBlank
  private String description;

  public Charity(Integer id, String name, String description) {
    this.id = id;
    this.name = name;
    this.description = description;
  }
  
  public Charity() {
  }

  public Integer getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public String getDescription() {
    return description;
  }

DAO method:

public void save(Charity charity) {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.saveOrUpdate(charity);
    session.getTransaction().commit();
    session.close();
  }

Service implementation:

  public void save(Charity charity){
    charityDao.save(charity);
  }
  @PostMapping("charities/charity")
  public String addCharity(@Valid @RequestBody Charity charity){ 
    charityService.save(charity);
    
    return "addForm";
  } 

validate JSON data and inspect for page and check for requests from the network

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM