Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error resolving template, when I am not even asking for one

So I am getting a very odd issue. When sending a post request using ajax to my Spring Controller, even though I am not returning anything, I am getting the following error.

Error resolving template "poliza/modificar-distribucion", template might not exist or might not be accessible by any of the configured Template Resolvers

Here is my Controller's code.

@Controller
@RequestMapping("/poliza")
public class EntryController {

    // Some other methods and the @Autowired services.

    @PostMapping(value = "/modificar-distribucion")
    public void updateEntryDistribution(@RequestBody EntryDistribution entryDistribution) throws Exception {
        entryDistributionService.updateDistribution(entryDistribution);
    }

}

This is my jQuery code.

// Return the distribution object for each cell.
function getDistributionObject(cell) {

    // Some logic.

    // Return the object.
    return {
        entryDistributionID: {
            entryID: id,
            productCode: productCode,
            sizeFK: size,
            sizeType: sizeType,
            storeId: storeId
        },
        quantity: integerValue,
        entryHeader: null
    }

}

function sendRequest(distributionObject, cell) {

    // Some logic.

    $.ajax({
            url: "/poliza/modificar-distribucion",
            data: JSON.stringify(distributionObject),
            contentType : 'application/json; charset=utf-8',
            dataType: 'json',
            type: "POST",
            success: function() {
                // Now, let's set the default value to the actual value.
                $(cell).attr('default-value', quantity);
            }, error: function(error) {
                // Send a notification indicating that something went wrong.
                // Only if it is not an abort.
                if(error.statusText === "Abort") return;
                errorNotification(error.responseJSON.message);
            }
    }));
}

My service code.

@Override
public void updateDistribution(EntryDistribution entryDistribution) throws Exception {

    // First, let's see if the distribution exists.
    EntryDistribution oldEntryDistribution = this.findById(entryDistribution.getEntryDistributionID());
    if(oldEntryDistribution == null) {
        // If not, insert it.
        this.insert(entryDistribution);
    } else {
        // Else, update it.
        this.update(entryDistribution);
    }

}

Entry distribution object.

public class EntryDistribution {

    private EntryDistributionID entryDistributionID;
    private Integer quantity;
    private EntryHeader entryHeader;

    // Getters and setters.
}

public class EntryDistributionID implements Serializable {

    private Long entryID;
    private String productCode;
    private String sizeFK;
    private String sizeType;
    private String storeId;

    // Getters and setters.
}

Any idea why this is happening? I shouldn't be getting this error, since I am not trying to fetch any Thymeleaf template in this particular call.

like image 935
Alain Cruz Avatar asked Oct 29 '25 05:10

Alain Cruz


2 Answers

Your method should return something instead of void so that ajax can know whether the call was successful or not(change method return type from void to boolean or string).

Because you have not specify the response content type so spring is trying to find html page.

To resolve that tell spring to return JSON response by adding @ResponseBody annotation on top of method like below.

@PostMapping(value = "/modificar-distribucion")
@ResponseBody
public String updateEntryDistribution(@RequestBody EntryDistribution entryDistribution) throws Exception {
        entryDistributionService.updateDistribution(entryDistribution);
        return "success";
}
like image 75
Alien Avatar answered Oct 30 '25 22:10

Alien


can you replace your code with this and try.

@PostMapping(value = "/modificar-distribucion")
public void updateEntryDistribution(@RequestBody EntryDistribution entryDistribution) throws Exception {
    entryDistributionService.updateDistribution(entryDistribution);
}



@ResponseBody
@RequestMapping(value = "/modificar-distribucion", method = RequestMethod.POST)
public Boolean updateEntryDistribution(@RequestBody EntryDistribution entryDistribution) {
    return true;
}

also check JSON object which you are passing must be same as POJO attributes

like image 30
Negi Rox Avatar answered Oct 31 '25 00:10

Negi Rox