收到错误HTTP状态415内容类型'application / x-www-form-urlencoded'不支持

时间:2019-01-17 12:04:43

标签: jquery ajax spring spring-mvc

after submitting form i'm getting this error -HTTP Status 415 [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported].
Note: Form is submitting and data is also saving into table when i remove the @RequestBody but the problem is response is not coming back to the jsp page. the message is printing in browser. i want to use jackson json library only.
My Code:

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                    <form action="subscribers/saveSubscribers" method="post"
                        id="subscribersForm">
                        <div class="subscribe">
                            <input type="email" class="form-control" name="email" id="email"
                                placeholder="Enter your Email" required=""> <input
                                type="hidden" class="form-control" value="Dharmesh"
                                name="firstName" id="firstName"
                                placeholder="Enter your FirstName" required=""> <input
                                type="hidden" class="form-control" value="Mourya"
                                name="lastName" id="lastName" placeholder="Enter your LastName"
                                required="">
                            <button class="btn btn-common" type="submit">Subscribe</button>
                        </div>
                        <br>
                        <center>
                            <p id="success">Thank you for subscribing our Newsletter.</p>
                        </center>
                        <center>
                            <p id="exist" style="color: green;">You have been already
                                Subscribed.</p>
                        </center>
                        <center>
                            <p id="error" style="color: red;">Oops! Something Went
                                Wrrong.</p>
                        </center>
                    </form>
                </div>
<script type="text/javascript">
        $(document).ready(function() {
            $('#success').hide();
            $('#exist').hide();
            $('#error').hide();
            $('#subscribersForm').submit(function() {
                var formData = $("#subscribersForm").serialize();
                alert(formData);
                data = JSON.stringify(formData);
                console.log(data);
                alert(data);
                $.ajax({
                    url : url,
                    type : 'POST',
                    traditional : true,
                    contentType : 'application/json',
                    data :JSON.stringify(formData),
                    dataType : 'json',
                    success : function(response) {
                        alert('success ' + response);
                        if ($.trim(response) == "success") {
                            $('#success').show();
                        } else if ($.trim(response) == "exist") {
                            $('#exist').hide();
                        } else {
                            $('#error').show();
                        }
                    },
                    error : function(response) {
                        alert('error ' + response);
                    },
                });

                return false;
            });
        });
    </script>

我的POJO课程:订阅者

@Entity
@Table(name="subscribers")
public class Subscribers {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")  
    Long id;

    @Column(name="firstname",nullable=false)
    @Size(max=50)
    @NotNull
    String firstName;

    @Column(name="lastname",nullable=false)
    @Size(max=50)
    @NotNull
    String lastName;

    @Column(name="email",nullable=false,unique=true)
    @Size(max=100)
    @NotNull
    String email;

    @Column(name="created_date")
    @NotNull
    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    Date createdDate;

    public Subscribers() {
        super();
    }
    //getters and setters

我的控制器类:SubscribersController

@Controller
@RequestMapping("/subscribers")
public class SubscribersController {
    private Logger logger = Logger.getLogger(SubscribersController.class);
    @Autowired
    private SubscribersService subscribersService;

    @RequestMapping(value = "/saveSubscribers", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveSubscribers(@RequestBody Subscribers subscribers) { 
        String msg=null;
        try {
            Date date = new Date();  
            Timestamp createdDate=new Timestamp(date.getTime());
            logger.info("Saving ......!"+subscribers.getFirstName()+" "+subscribers.getLastName()+" "+subscribers.getEmail());
            String firstName=subscribers.getFirstName();
            String lastName=subscribers.getLastName();
            String email=subscribers.getEmail();
            if(email!=null) {
            if(firstName=="" || firstName==null && lastName=="" || lastName==null) {
                firstName="-";
                lastName="-";
            }
            subscribers.setEmail(email);
            subscribers.setFirstName(firstName);
            subscribers.setLastName(lastName);  
            subscribers.setCreatedDate(createdDate);
            subscribersService.save(subscribers);
            msg="success";
            return msg;
            } else {
                msg="Email is required.";
                return msg; 
            }
        } catch (Exception e) { 
            logger.error("Error during saving Subscribers: "+e.getMessage());
            msg="error";
            return msg;
        }

    }

}

0 个答案:

没有答案