0

i am getting a 406 error while running the below code on my localhost:-

Controller Code:-

//imports
import AvailabilityReportService;
import SAvailabilityReport;
@Controller
@RequestMapping("/service/*")

public class SAvailabilityController {
    @Autowired
    @Qualifier("AvailabilityReportService")
    private AvailabilityReportService availabilityReportService;

    @RequestMapping(value = "/{cat}", method = RequestMethod.GET)
    public @ResponseBody
    SAvailabilityReport getAvailabilityReport(@PathVariable String cat) {
        try
        {   
            return availabilityReportService.generateAvailabilityReport(cat);
        } catch (Exception e) {

            e.printStackTrace();
            return null;
        }
    }


}

The Availability Report Service Class:-

import SAvailabilityReport;
public interface AvailabilityReportService {


    public SAvailabilityReport generateAvailabilityReport(String cat) throws Exception;
}

Now the availabilityreportserviceimpl class that implements the above class:-

//The required imports
@Service("AvailabilityReportService")
public class AvailabilityReportServiceImpl implements AvailabilityReportService {
    private @Value("${username}")
    String username;

    private @Value("${password}")
    String password;

    private @Value("${username1}")
    String username1;

    private @Value("${password1}")
    String password1;

    private PropertiesConfiguration propConfig;

    @PostConstruct
    public void initIt() throws Exception {
        String propertiesFile = "/META-INF/common.properties";
        propConfig = new PropertiesConfiguration(this.getClass().getResource(
                propertiesFile));
        propConfig.setReloadingStrategy(new FileChangedReloadingStrategy());
    }



 public SAvailabilityReport generateAvailabilityReport(String cat)
        throws Exception {
    String user;
    String pass;
    String time;
    if (cat == null)
        return null;
    String url = (String) propConfig.getProperty("s_"
            + cat.toLowerCase());
    if (url == null)
        return null;
    SAvailabilityReport availabilityReport = new SAvailabilityReport();
    if((cat.toLowerCase()).equals("h") || (cat.toLowerCase()).equals("f"))
    {
        user=username;
        pass=password;
    }
    else
    {
        user=username1;
        pass=password1;
    }
    WebClient webClient = provideCredentials(user,pass);
    HtmlPage resultPage = webClient.getPage(url);
    HtmlTable resultTable = (HtmlTable) resultPage.getByXPath(
            "//table[@class='data']").get(0);

    List<HtmlTableRow> rowsList = resultTable.getRows();
    int rowsSize = rowsList.size();
    time=(resultTable.getCellAt((rowsSize-1),1)).asText();


    availabilityReport.settime(time);
    return availabilityReport;

}

private WebClient provideCredentials(String username, String password) {
    WebClient webClient = new WebClient();
    DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient
            .getCredentialsProvider();
    credentialsProvider.addCredentials(username, password);
    return webClient;
}

Finally the savailabilityreport class is:-

//imports
public class SAvailabilityReport {

    private String time;


    public SAvailabilityReport()
    {

    }
    public void settime(String time)
    {
        this.time=time;
    }

}

I had done some research about entering the jackson-core-asl and jackson-mapper-asl into my pom.xml, and also .I have done those but still the same 406 error.. The url from which i am getting the value has been defined in the common.proprties file along with the username and password.

david419
  • 435
  • 3
  • 8
  • 18
  • What does your client look like i.e how are you testing this. 406 error relates to client's Accept header problem. Check it. – arahant Feb 23 '14 at 17:20
  • well i am testing by running a maven build then getting a war file, and deploying on a jetty server, then using the browser to check the output. How to check the accept headers of the client? i am a noob at this so please be precise. Thanks – david419 Feb 24 '14 at 04:01
  • also in the @RequestMapping if i set headers={"content-type=application/json"} it throws the 415 error, unsupported media type – david419 Feb 24 '14 at 04:39

1 Answers1

0

problem solved it was just a case of not having setters in the formatter class. as soon as i put the setters in my formatter class it started to work.

david419
  • 435
  • 3
  • 8
  • 18