0

I have started learning SpringBoot and created some of the APIs.

I've the following API Controller

@RestController
@RequestMapping("/api")
public class ItemRestAPIController {    
    @Autowired
    private ItemService itemService;

    @PostMapping("/create")
    @ResponseStatus(HttpStatus.CREATED)
    public void addItem(@RequestBody Items item)
    {
        itemService.saveItem(item);
    }
}  

This works absolutely fine via POSTMAN and data gets inserted into mongodb collection.

I've another controller to add the data to mongodb via a HTML using thymeleaf.

@Controller
@RequestMapping("/views")
public class ItemViewController {
    
    @Autowired
    private ItemService itemService;


    @ResponseBody
    @PostMapping("/saveItem")
    @ResponseStatus(HttpStatus.CREATED)
    public String saveItem(Items item)
    {
        System.out.println("Received Data in saveitem function:" + item);
        itemService.saveItem(item);
        return "Item Entered into the Database...";
    }
}    

Upon entering the data in the form I get the following in the output :

Received Data in saveitem function:Items[id=null, name='null', cost=0]

and empty data was inserted into mongodb collection.

After going through some articles on stackoverflow and others I did the following :

@ResponseBody
@PostMapping("/saveItem")
@ResponseStatus(HttpStatus.CREATED)
public String saveItem(@RequestBody Items item)
{
    System.out.println("Received Data in saveitem function:" + item);
    itemService.saveItem(item);
    return "Item Entered into the Database...";
}

Then i got the following error:

2022-01-25 12:05:46.216 WARN 31052 --- [nio-9090-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

Again after going through some other article I did the following:

@ResponseBody
@RequestMapping(value = "/saveItem" , method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
        produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
@ResponseStatus(HttpStatus.CREATED)
public String saveItem(Items item)
{
    System.out.println("Received Data in saveitem function:" + item);
    itemService.saveItem(item);
    return "Item Entered into the Database...";
}

and I was still getting the same null result on the console.

Received Data in saveitem function:Items[id=null, name='null', cost=0]

add-item.html using thymeleaf is

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div align="center">
    <form th:action="@{/views/saveItem}" th:object="${item_data}" method="post">
        <p><input th:type="hidden"  field="*{id}" /></p>
        <p>Name: <input th:type="text" field="*{name}" /></p>
        <p>Cost: <input th:type="text" field="*{cost}" /></p>
        <input th:type="submit" th:value="Submit"/><br>
    </form>
</div>
</body>
</html>

The Items class is as follows:

public class Items {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "_id", nullable = false)
    private BigInteger id;

    private String name;
    private int cost;

    public Items() {
    }

    public Items(BigInteger id, String name, int cost) {
        this.id = id;
        this.name = name;
        this.cost = cost;
    }
}

I'm sorry for the long post, but I'm unable to get the result.

0 Answers0