Encountered a problem while attempting to generate JSON data on Spring 3.2, resulting in a "Page not found 404" error

I am currently working on retrieving data from a database using Sprng 3.2 and myBatis with the goal of receiving the data in JSON format.

In order to achieve this, I have included jackson-core-asl, jackson-core-lgpl, and jackson-mapper-asl(1.9.13) in the pom.xml file.

However, upon sending a request to the server, I encountered an error message stating "HTTP Status 404 - /WEB-INF/jsp/listJson.jsp". This issue has caused me to question why my configuration file is not functioning properly for @ResponseBody.

Despite conducting extensive research online, I have been unable to resolve the error thus far. Could it be possible that this setup does not work as expected on Spring 3.2?

Interestingly, when I replaced <mvc:annotation-driven /> with the following code snippet:

<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
    <beans:list>
        <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    </beans:list>
</beans:property>

This change led to successful results. However, it should be noted that AnnotationMethodHandlerAdapter is deprecated in Spring 3.2.

  1. servlet-context.xml

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

2 Controller

@Controller
public class BoardController
{
    @Autowired
    private MainService mainService;

    @RequestMapping("/listJson.do")
    public @ResponseBody
        Map<?, ?> listJson(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable
    {
        model.put("results", mainService.getList(paramMap));
        return model;
    }
}

Answer №1

After much trial and error, I have finally found a configuration that works perfectly for me!

Simply including <mvc:annotation-driven> is not sufficient to render data in JSON format. Upon inspecting the tomcat log console, I discovered that the HandlerMapping being used was RequestMappingHandlerMapping.

<mvc:annotation-driven />

<beans:bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <beans:property name="mediaTypes">
        <beans:map>
            <beans:entry key="html" value="text/html" />
            <beans:entry key="json" value="application/json" />
        </beans:map>
    </beans:property>
    <beans:property name="viewResolvers">
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
    </beans:property>
    <beans:property name="defaultViews">
        <beans:list>
            <beans:bean
                class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
        </beans:list>
    </beans:property>
</beans:bean>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Surprise integer appearing in the JSON payload's closing

I'm currently working on a sample demo that involves the use of PHP and AJAX to read a JSON file. In my JavaScript code, I have: // main entry point function init(){ var button = document.getElementById("button"); button.addEventListener("clic ...

Using NextJS to navigate user journey by mapping an array of values from Formik to

I really appreciate all the help I've received so far, but I'm facing an issue trying to map an object with an array within it to the router. Here is my current code: const initialValues = { region: query.region || 'all', campt ...

Jasmine spies call through problem detected

I can't seem to get my code to work properly and I keep encountering this error: TypeError: undefined is not an object (evaluating 'GitUser.GetGitUser('test').then') ... Check out the code snippets below: app.controller(&apo ...

How to send a JavaScript variable to Flask and trigger an AJAX reload action

Introduction: I have explored similar inquiries and attempted to apply relevant code/concepts but without success. -https://stackoverflow.com/questions/43645790/passing-javascript-variable-to-python-flask -https://stackoverflow.com/questions/10313001/is- ...

Loop through the array and print out only the last item

I am struggling to get my for loop to print out every item in the array, instead of just the last item. Can't seem to figure out where I'm going wrong: var patients = ["Julia", "Kelly", "Thomas", "Clare"]; function lineOfPatients(line) { if (! ...

Sequencing numerous promises (managing callbacks)

I am encountering some challenges with promises when it comes to chaining multiple ones. I'm having difficulty distinguishing how to effectively utilize promises and their differences with callbacks. I've noticed that sometimes callbacks are trig ...

Utilize Require.js to Load Dropzone.js Library

I am interested in integrating Dropzone.js into an application that is built using Backbone and Require.JS. However, I am unsure of the correct implementation process. Should I utilize require()? What is the most effective way to handle this integration? ...

Struggling with incorporating a lightbox within an accordion feature in an .html file

I'm currently attempting to integrate this lightbox into this accordion feature. Individually, I've managed to get both elements up and running smoothly. However, when trying to combine them on the same page (regardless of nesting one inside the ...

Adding 7 days to a JavaScript date

Can you spot the bug in this script? I noticed that when I set my clock to 29/04/2011, it displays 36/4/2011 in the week input field! The correct date should actually be 6/5/2011 var d = new Date(); var curr_date = d.getDate(); var tomo_date = d.getDate( ...

Dropdown selection triggers an Ajax request

I have encountered an issue while attempting to make an Ajax request to the page chosen in a drop-down menu. While most of my script code works fine in binding a mouse click to table rows, it seems to fail in this particular scenario. The error message I r ...

Unable to retrieve any files using NEST from elasticsearch

I rely on Searchblox for indexing and searching my files, leveraging ES 2.x for the task. Using a "mapping.json" file, Searchblox initializes mappings when creating an index. You can access the file here. Following suggestions from "@Russ Cam" here, I deve ...

How come the useState function remains undefined, even when initialized with a default value?

I attempted to set an empty array as the default value for a state using the useState hook in React. However, when I try to check the type of testing with console.log(Array.isArray(testing)); or simply log testing, it always displays undefined regardless o ...

Setting an icon as a binding in a dropdown menu using Vue Js

Looking for some guidance with Vue.js and adding Font Awesome icons to dropdown items. I am a beginner with Vue.js and I'm struggling with binding icons after each dropdown item. Below is the code I am currently using: <select id="testID" ...

Exploring Django and Json with conditional statements

I am working on a script that adds table data from a JSON source. Here is the code: <script> const tableData = $('#table-data').DataTable({ dom: 'Bfrtip', "serverSide": true, &qu ...

The Mongoose _id seems to be malfunctioning when a custom value is used instead of the default value

Greetings! I am currently working with Mongoose and have a collection named 'tag' with the following schema: const tagSchema = new Schema<ITag, ITagModel>( { _id: { type: String, unique: true, required: true, }, ...

Discovering the expected parameters of a function

My function looks like this: function myFunction(params) { // TODO: something console.log(params.message) } I want to find out all the keys that the 'myFunction' function expects in the params object. Can this be done? I attempted to use ht ...

Mastering Nested ModelSerializers in Django REST: A Step-by-Step Guide to POST and PUT Data

As a beginner in Django, I am already able to fetch data. Here is an example of some JSON: { "pk": 4, "account": "user4", "password": "5340", "UserDatas": { "memberAccount_id": 4, "username": "Dylan", "gender": true ...

What are some tips for utilizing the "bottom-row" slot within the "b-table" component in bootstrap-vue?

I am working on a component that utilizes the bootstrap-vue b-table component. My goal is to create a bottom row that displays the sum of each column in the table. However, I encountered an issue where the bottom-row only fills the first column, leaving ...

Is JSX Babel being rejected for execution?

I have recently started learning React and encountered an error while trying to run this page: Refused to execute script from 'https://unpkg.com/browse/[email protected]/babel.min.js' because its MIME type ('text/html') is not exec ...

WCF service method does not support ajax calls

When I make a simple ajax call with a WCF method, it returns bad-request, but without the method, it shows the status as success. Ajax Call $(document).ready(function(){ $.get("http://localhost:1347/Service1.svc/DoWork", //this shows bad-requ ...