POJO to Web Service through Controller in Spring Boot is not working
<p>I am working with Spring Boot service client project. I have a JSP file, a POJO, Controller and Web Service classes. They are given below. </p>

<p>My problem is that whenever I call the WS from JSP through client Controller, it is not working. However, it is working when it gets called directly from the method. Also, it works when removing <code>@Valid</code> from service method.</p>

<p><strong>JSP file:</strong></p>

<pre><code>&lt;%@ page import="test.model.CardType" %&gt;
&lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;

&lt;!-- ... --&gt;

&lt;form:hidden path="id" /&gt;
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;label&gt;Card Number:&lt;/label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path="number" placeholder="Card Number" /&gt;&lt;/td&gt;
            &lt;/tr&gt;

            &lt;tr&gt;
                &lt;form:select path="type"&gt;
                    &lt;form:options items="${cards}" /&gt;
                &lt;/form:select&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;label&gt;Year:&lt;/label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path="year" placeholder="Year" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;label&gt;Month:&lt;/label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path="month" placeholder="month" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;label&gt;Security Code:&lt;/label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path="code" placeholder="Code" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;label&gt;&lt;/label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;input type="submit" value="Save" class="save" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
&lt;/form:form&gt;
</code></pre>

<p><strong>Mapping Function:</strong></p>

<pre><code>@PostMapping("/checkout")
public String checkOut(@ModelAttribute("card") Card card) {
    /** Card card2 = new Card(); //problem is in here: if I sent card2 it works, but not with card
    card2.setId(1);
    card2.setCode(123);
    card2.setMonth(123);
    card2.setNumber("1111222233334444");
    card2.setType(CardType.DEBIT_CARD);
    card2.setYear(1990);
    System.out.println("========= ====================================" + "\n" + card.getId() + "\n"
            + card.getCode() + "\n" + card.getMonth() + "\n" + card.getNumber() + "\n" + card.getYear() + "\n"
            + card.getType() + "-------------------------------");*/

    final String uri = "http://localhost:8090/cardservice";

    RestTemplate restTemplate = new RestTemplate();

    Card result = restTemplate.postForObject(uri, card, Card.class);
    System.out.println(result.getNumber() + "\n" + result.getType());

    return "redirect:/card/form";
}
</code></pre>

<p><strong>POJO:</strong></p>

<pre><code>@Entity
public class Card {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String number;

    @Enumerated(EnumType.STRING)
    private CardType type;
    private int year;
    private int month;
    private int code;

    // Getters/Setters

}
</code></pre>

<p><strong>Service:</strong></p>

<pre><code>@ResponseBody
@PostMapping("/cardservice")
public Card getDetails(@Valid @RequestBody Card card, BindingResult br) {
    if(br.hasErrors()){
         throw new IllegalArgumentException("Please enter valid credit card number");
    }

    return card;
}
</code></pre>

<p>In service, <code>Card</code> domain is the same as in client, but I used <code>@CreditCardNumber</code> to validate credit card number.</p>

<p><strong>Error:</strong></p>

<pre><code>2016-11-22 00:18:37.784[0;39m [31mERROR[0;39m [35m501[0;39m [2m---[0;39m [2m[nio-8080-exec-6][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet]   [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is     

org.springframework.web.client.HttpServerErrorException: 500 null] with    
root cause
</code></pre>